Coder Social home page Coder Social logo

wafer-node-session's Introduction

微信小程序 Node 会话管理中间件

wafer-node-session 处理来自小程序的登录请求并建立和维护会话。

获取与安装

npm install wafer-node-session --save

使用

const express = require('express');
const session = require('wafer-node-session');
const RedisStore = require('connect-redis')(session);

app.use(session({
    // 小程序 appId
    appId: '...',

    // 小程序 appSecret
    appSecret: '...',

    // 登录地址
    loginPath: '/login',

    // 会话存储
    store: new RedisStore({ ... })
}));

app.use('/me', function(request, response, next) {
    if (request.session) {
        // 从会话获取用户信息
        response.json(request.session.userInfo);
    } else {
        response.json({ nobody: true });
    }
})

上面的会话存储使用了 connect-redis,实际上,wafer-node-session 支持 express-session 的 Store,具体可参考这个列表

客户端配合

小程序需要使用 wafer-client-sdk 发起请求才能正常建立会话,请在客户端进行引入。

npm install -g bower
bower install wafer-client-sdk

客户端发起请求代码如下:

var wafer = require('./bower_components/wafer-client-sdk/index');
var HOST = 'my.example.com';

// 登录地址,注意路径要和中间件配置的 loginPath 一直
wafer.setLoginUrl(`https://${HOST}/login`);
wafer.request({
    login: true,
    url: `https://${HOST}/user`,
    success: function(response) {
        console.log(response);
    },
    fail: function(err) {
        console.log(err);
    }
});

wafer-node-session's People

Contributors

techird avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.