簡體   English   中英

如何結束用戶 session 令牌並在 angular 中使用超過 25 分鍾的時間刷新令牌

[英]how to end an user session token and refresh the token with the time is more than 25 minutes in angular

我正在使用 angular 和 nodejs。 在 Nodejs 中,我使用的是 jwt-simple 庫。

如何在用戶處於非活動狀態超過 25 分鍾時結束 session 並顯示消息以確認注銷或刷新令牌。

中間件

'use strict'

var jwt = require('jwt-simple');
var moment = require('moment');


var secret = 'clave-secreta-para-generar-el-token-9999';


exports.authenticated = function(req, res, next) {

    // Comprobar si llega autorización
    if (!req.headers.authorization) {
        return res.status(403).send({
            message: 'La petición no tiene la cabecera de authorization'
        });
    }

    // Limpiar el token y quitar comillas
    var token = req.headers.authorization.replace(/['"]+/g, '');

    try {
        // Decodificar token
        var payload = jwt.decode(token, secret, false, 'HS256');
        console.log(payload);
      

        // Comprobar si el token ha expirado
        if (payload.exp <= moment(1 * 1).unix()) {
            return res.status(404).send({
                message: 'El token ha expirado'
            });
        }

    } catch (ex) {
        return res.status(404).send({
            message: 'El token no es válido'
        });
    }

    // Adjuntar usuario identificado a request
    req.useradmin = payload;

    // Pasar a la acción
    next();

};

服務 jwt

'use strict'

var jwt = require('jwt-simple');
var moment = require('moment');

exports.createToken = function(user) {

    var payload = {
        sub: user._id,
        name: user.name,
        surname: user.surname,
        email: user.email,
        role: user.role,
        iat: moment().unix(),
        exp: moment().add(2, 'minutes').unix
    };

    return jwt.encode(payload, 'clave-secreta-para-generar-el-token-9999');
};

令牌永不過期。 它始終在 localStorage 中。

你可以試試這樣的

import { of } from "rxjs";
import { delay } from "rxjs/operators";

const delayInMs = 25 * 60 * 1000;
const alert25minutes$ = of('alert').pipe(delay(delayInMs));
alert25minutes$.subscribe(() => openAlertModal());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM