|
|
@@ -1,28 +1,25 @@
|
|
|
+import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
|
+import { InjectConnection } from '@nestjs/typeorm';
|
|
|
import * as jwt from 'jsonwebtoken';
|
|
|
-import { Injectable, UnauthorizedException } from "@nestjs/common";
|
|
|
-import { JwtPayload } from "./auth-types";
|
|
|
-import { Role } from "./role";
|
|
|
-import { PasswordService } from "./password.service";
|
|
|
-import { Connection } from "typeorm";
|
|
|
-import { InjectConnection } from "@nestjs/typeorm";
|
|
|
-import { UserEntity } from "../entity/user/user.entity";
|
|
|
+import { Connection } from 'typeorm';
|
|
|
+import { UserEntity } from '../entity/user/user.entity';
|
|
|
+import { JwtPayload } from './auth-types';
|
|
|
+import { PasswordService } from './password.service';
|
|
|
+import { Role } from './role';
|
|
|
|
|
|
// TODO: make this configurable e.g. from environment
|
|
|
export const JWT_SECRET = 'some_secret';
|
|
|
|
|
|
@Injectable()
|
|
|
export class AuthService {
|
|
|
+ constructor(private passwordService: PasswordService, @InjectConnection() private connection: Connection) {}
|
|
|
|
|
|
- constructor(private passwordService: PasswordService,
|
|
|
- @InjectConnection() private connection: Connection) {}
|
|
|
-
|
|
|
- async createToken(identifier: string, password: string): Promise<{ user: UserEntity; token: string; }> {
|
|
|
- const user = await this.connection.getRepository(UserEntity)
|
|
|
- .findOne({
|
|
|
- where: {
|
|
|
- identifier
|
|
|
- }
|
|
|
- });
|
|
|
+ async createToken(identifier: string, password: string): Promise<{ user: UserEntity; token: string }> {
|
|
|
+ const user = await this.connection.getRepository(UserEntity).findOne({
|
|
|
+ where: {
|
|
|
+ identifier,
|
|
|
+ },
|
|
|
+ });
|
|
|
|
|
|
if (!user) {
|
|
|
throw new UnauthorizedException();
|
|
|
@@ -33,7 +30,7 @@ export class AuthService {
|
|
|
if (!passwordMatches) {
|
|
|
throw new UnauthorizedException();
|
|
|
}
|
|
|
- const payload: JwtPayload = { identifier , roles: user.roles };
|
|
|
+ const payload: JwtPayload = { identifier, roles: user.roles };
|
|
|
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: 3600 });
|
|
|
|
|
|
return { user, token };
|
|
|
@@ -42,8 +39,8 @@ export class AuthService {
|
|
|
async validateUser(payload: JwtPayload): Promise<any> {
|
|
|
return await this.connection.getRepository(UserEntity).findOne({
|
|
|
where: {
|
|
|
- identifier: payload.identifier
|
|
|
- }
|
|
|
+ identifier: payload.identifier,
|
|
|
+ },
|
|
|
});
|
|
|
}
|
|
|
}
|