import { Provide , Plugin , Inject , Config } from '@midwayjs/decorator' ; import { InjectEntityModel } from '@midwayjs/orm' ; import { JwtComponent } from '@mw-components/jwt' ; import { Redis } from 'ioredis' ; import { Repository } from 'typeorm' ; import { Context } from '@/interface' ; import { JwtAuthMiddlewareConfig } from '../../config/config.types' ; import { AdminUserModel } from '../model/admin-user' ; @ Provide ( ) export class AuthService { @ Inject ( ) private ctx : Context ; @ Inject ( 'jwt:jwtComponent' ) jwt : JwtComponent ; @ Config ( 'jwtAuth' ) private jwtAuthConfig : JwtAuthMiddlewareConfig ; @ InjectEntityModel ( AdminUserModel ) private adminUserModel : Repository < AdminUserModel > ; @ Plugin ( ) private redis : Redis ; param { AdminUser } data returns { String } async createAdminUserToken ( data ) { const token = this . jwt . sign ( { id : data . id } , '' , { expiresIn : this . jwtAuthConfig . accessTokenExpiresIn , } ) ; await this . redis . set ( `${ this . jwtAuthConfig . redisScope }:accessToken:${ data . id }` , token , 'EX' , this . jwtAuthConfig . accessTokenExpiresIn ) ; return token ; } param { String } id returns { String } async getAdminUserTokenById ( id ) { return this . redis . get ( `${ this . jwtAuthConfig . redisScope }:accessToken:${ id }` ) ; } param { String } id returns { number } async removeAdminUserTokenById ( id ) { return this . redis . del ( `${ this . jwtAuthConfig . redisScope }:accessToken:${ id }` ) ; } param { String } username returns { AdminUserModel | null } async getAdminUserByUserName ( username ) { const user = await this . adminUserModel . findOne ( { where : { username , } , } ) ; return user ; } param { String } id returns { AdminUserModel } public async getAdminUserById ( id ) { const userinfo = ( await this . redis . get ( `${ this . jwtAuthConfig . redisScope }:userinfo:${ id }` ) ) as string ; return JSON . parse ( userinfo ) as AdminUserModel ; } param { AdminUserModel } data returns { OK | null } async cacheAdminUser ( data ) { const { id , username , name , avatar , createdAt , updatedAt } = data ; const userinfo = { id , username , name , avatar , createdAt , updatedAt , } ; return this . redis . set ( `${ this . jwtAuthConfig . redisScope }:userinfo:${ userinfo . id }` , JSON . stringify ( userinfo ) , 'EX' , this . jwtAuthConfig . accessTokenExpiresIn ) ; } param { String } id returns { number } async cleanAdminUserById ( id ) { return this . redis . del ( `${ this . jwtAuthConfig . redisScope }:userinfo:${ id }` ) ; } param { Object } params returns { AdminUserModel | null } async localHandler ( params ) { const getAdminUser = ( username ) => { return this . getAdminUserByUserName ( username ) ; } ; const existAdmiUser = await getAdminUser ( params . username ) ; if ( ! existAdmiUser ) { return null ; } const passhash = existAdmiUser . password ; const equal = this . ctx . helper . bcompare ( params . password , passhash ) ; if ( ! equal ) { return null ; } return existAdmiUser ; } }