ค้นหาเว็บไซต์

typescript - คุณจะดำเนินการยืนยันตัวตน Superlogin-Client ที่ด้านหน้าด้วย Superlogin, ionic3, PouchDB และ CouchDB ได้อย่างไร?


ฉันไม่รู้ว่าจะต้องวางการนำเข้าและการกำหนดค่า superlogin-client ไว้ที่ไหน และจะเก็บโทเค็นการยืนยันตัวตนลงใน local storage ได้อย่างไร เพื่อให้แอปมือถือสามารถยังคงเข้าสู่ระบบได้แม้ออฟไลน์

ฉันได้ลองสิ่งนี้แล้วแต่ก็ไม่ทำงาน:

    import PouchDB from 'pouchdb';
    import superlogin from 'superlogin-client';

    var config = {
      // The base URL for the SuperLogin routes with leading and trailing slashes (defaults to '/auth/')
      baseUrl: 'http://localhost:3000/auth/',
      // A list of API endpoints to automatically add the Authorization header to
      // By default the host the browser is pointed to will be added automatically
      endpoints: ["localhost:3000"],
      // Set this to true if you do not want the URL bar host automatically added to the list
      //noDefaultEndpoint: false,
      // Where to save your session token: localStorage ('local') or sessionStorage ('session'), default: 'local'
      storage: 'local',
      // The authentication providers that are supported by your SuperLogin host
      providers: ['facebook', 'twitter'],
      // Sets when to check if the session is expired. 'stateChange', 'startup' or nothing.
      // 'stateChange' checks every time $stateChangeStart or $routeChangeStart is fired
      // 'startup' checks just on app startup. If this is blank it will never check.
      checkExpired: false,
      // A float that determines the percentage of a session duration, after which SuperLogin will automatically refresh the
      // token. For example if a token was issued at 1pm and expires at 2pm, and the threshold is 0.5, the token will
      // automatically refresh after 1:30pm. When authenticated, the token expiration is automatically checked on every
      // request. You can do this manually by calling superlogin.checkRefresh(). Default: 0.5
      refreshThreshold: 0.5
    };

    superlogin.configure(config);
    /*
      Generated class for the StoresProvider provider.

      See https://angular.io/guide/dependency-injection for more info on providers
      and Angular DI.
    */
    @Injectable()
    export class StoresProvider {
      db: any;
      remote: any;

      mystores: any = {};
      constructor() {
        console.log('Hello StoresProvider Provider');}

คำตอบ #1

ดูเหมือนว่าคุณกำลังพยายามเรียกใช้ superlogin จากฝั่งไคลเอนต์ (Ionic) อย่างไรก็ตาม ฉันเชื่อว่าการใช้งาน superlogin ควรอยู่ที่ฝั่งเซิร์ฟเวอร์ และแอป Ionic ของคุณควรส่งคำขอสมัครสมาชิกไปยัง Node Server ของคุณ ซึ่งจากนั้นจะทำการตรวจสอบสิทธิ์โดยใช้ superlogin

นี่คือตัวอย่างการใช้งานเซิร์ฟเวอร์ Node

var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var logger = require('morgan');
var cors = require('cors');
var SuperLogin = require('superlogin');
var userRoutes=require('./app/user.route'); 
var app = express();

app.set('port', process.env.PORT || 3000);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'DELETE, PUT, OPTIONS,POST,GET');
res.header("Access-Control-Allow-Headers", "Authorization, Origin, X- 
Requested-With, Content-Type, Accept");
next();
});

var config = {
dbServer: {
protocol: 'http://',
host: '13.175.228.159:5984',  //should be url of couchdb server
user: 'couchadmin',
password: 'Passw0rd',
couchAuthDB: '_users',
userDB:'sl_users'
},
defaultSecurityRoles: {
members: ['merchant']
},
 mailer: {
  fromEmail: '[email ',
   options:{
    service: 'Gmail',
     auth: {
      user: '[email ',
      pass: 'simplepass'
    }
 }
},
security: {
  maxFailedLogins: 3,
  lockoutTime: 600,
  tokenLife: 86400,
  loginOnRegistration: true,
   defaultRoles:['merchant','user']
    },
    providers: {
   local: true
  }
}


var superlogin = new SuperLogin(config);

//enable cors preflight for all options.
app.options('*', cors()) // include before other routes 

// Mount SuperLogin's routes to our app
app.use('/auth', superlogin.router);
app.use('/user',userRoutes,superlogin.requireAuth);

app.listen(app.get('port'));
console.log("App listening on " + app.get('port'));