zoukankan      html  css  js  c++  java
  • jwt 登录

    /* eslint-disable */
    'use strict';
    
    const Controller = require('egg').Controller;
    const jwt = require('jsonwebtoken');
    
    class LoginController extends Controller {
      async index() {
        const ctx = this.ctx;
        const token = jwt.sign({
          user_id: 1,
          user_name: ctx.request.body.username
        }, 'shenzhouhaotian', {
            expiresIn: '10000s'
        });
        ctx.body = {
          token: token,
          user_name:ctx.request.body.username
        };
        ctx.status = 200;
      }
    }
    
    module.exports = LoginController;
    

      

    import Axios from 'axios'
    import router from '../router'
    
    if (window.localStorage.getItem('token')) {
      Axios.defaults.headers.common['Authorization'] = `Bearer ` + window.localStorage.getItem('token')
    }
    
    export let instance = Axios.create({
      baseURL: process.env.NODE_ENV === 'production' ? 'http://sayhub.me/api' : 'http://localhost:7001'
    })
    // respone拦截器
    instance.interceptors.response.use(
      response => {
        return response
      },
      error => {
        if (error.response) {
          switch (error.response.status) {
            case 401:
              router.replace({
                path: 'login',
                query: { redirect: router.currentRoute.fullPath } // 将跳转的路由path作为参数,登录成功后跳转到该路由
              })
          }
        }
        return Promise.reject(error.response)
      }
    )
    
    export const login = ({ loginUser, loginPassword }) => {
      return instance.post('/login', {
        username: loginUser,
        password: loginPassword
      })
    }
    
    export const getUserInfo = () => {
      return instance.get('/profile')
    }
  • 相关阅读:
    省队集训Ⅱ-Day5
    省队集训Ⅱ-Day4
    省队集训Ⅱ-Day3
    省队集训Ⅱ-Day2
    省队集训Ⅱ-Day1
    并查集 Pro
    树上带修: 莫队Ⅳ
    树上骗分: 莫队Ⅲ
    带修骗分: 莫队Ⅱ
    骗分带师: 莫队Ⅰ
  • 原文地址:https://www.cnblogs.com/qq735675958/p/10182716.html
Copyright © 2011-2022 走看看