@mvx/cli
Version:
61 lines (48 loc) • 1.61 kB
text/typescript
import { Inject } from '@tsdi/ioc';
import { Controller, Post, ContextToken, IContext, Cors, Get, Authorization } from '@mvx/mvc';
import { JwtStrategy, IAuthenticator } from '@mvx/identity';
import { ResponseResult } from '../ResponseResult';
import { UserRepository } from '../repositories/UserRepository';
export class AuthController {
private ctx: IContext;
private rep: UserRepository;
constructor() {
}
async login() {
let body = this.ctx.request.body;
let { user, info } = await this.rep.verify(body.username, body.password);
if (user) {
let strategy = this.ctx.passport.get('jwt') as JwtStrategy;
let sign = await strategy.sign({
data: user.id.toString()
}, strategy.secretOrKey, { expiresIn: 60 * 60 * 1000 });
return ResponseResult.success(sign);
} else {
return this.ctx.throw(401, info);
}
}
async refreshToken() {
let strategy = this.ctx.passport.get('jwt') as JwtStrategy;
let sign = await strategy.sign({
data: this.ctx.getUser().id
}, strategy.secretOrKey, { expiresIn: 60 * 60 * 1000 });
return ResponseResult.success(sign);
}
profile() {
return ResponseResult.success(this.ctx.getUser());
}
logout() {
this.ctx.logout();
return ResponseResult.success();
}
}