Added file/json logging.
This commit is contained in:
@ -8,10 +8,16 @@ import { JwtRefreshGuard } from './guards/jwt-refresh.guard';
|
||||
import { OfflineGuard } from './guards/offline.guard';
|
||||
import { UserEntity } from 'src/users/users.entity';
|
||||
import { QueryFailedError } from 'typeorm';
|
||||
import { PinoLogger } from 'nestjs-pino';
|
||||
import { JwtAccessGuard } from './guards/jwt-access.guard';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(private auth: AuthService, private users: UsersService) { }
|
||||
constructor(
|
||||
private auth: AuthService,
|
||||
private users: UsersService,
|
||||
private logger: PinoLogger,
|
||||
) { }
|
||||
|
||||
@UseGuards(LoginAuthGuard)
|
||||
@Post('login')
|
||||
@ -19,57 +25,53 @@ export class AuthController {
|
||||
@Request() req,
|
||||
@Res({ passthrough: true }) response: Response,
|
||||
) {
|
||||
let data: AuthenticationDto | null;
|
||||
try {
|
||||
let data: AuthenticationDto | null;
|
||||
try {
|
||||
data = await this.auth.login(req.user);
|
||||
if (!data.access_token || !data.refresh_token || !data.refresh_exp) {
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'Something went wrong with tokens while logging in.',
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof QueryFailedError) {
|
||||
if (err.message.includes('duplicate key value violates unique constraint "users_user_login_key"')) {
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'Username already exist.',
|
||||
};
|
||||
}
|
||||
}
|
||||
console.log('AuthController', typeof err, err);
|
||||
data = await this.auth.login(req.user);
|
||||
if (!data.access_token || !data.refresh_token || !data.refresh_exp) {
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'Something went wrong while logging in.',
|
||||
error_message: 'Something went wrong with tokens while logging in.',
|
||||
};
|
||||
}
|
||||
|
||||
response.cookie('Authentication', data.access_token, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
expires: new Date(data.exp),
|
||||
});
|
||||
|
||||
response.cookie('Refresh', data.refresh_token, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
expires: new Date(data.refresh_exp),
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
this.logger.error({
|
||||
class: AuthController.name,
|
||||
method: this.login.name,
|
||||
msg: 'Failed to login.',
|
||||
error: err,
|
||||
});
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'Something went wrong.',
|
||||
error_message: 'Something went wrong while logging in.',
|
||||
};
|
||||
}
|
||||
|
||||
response.cookie('Authentication', data.access_token, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
expires: new Date(data.exp),
|
||||
});
|
||||
|
||||
response.cookie('Refresh', data.refresh_token, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
expires: new Date(data.refresh_exp),
|
||||
});
|
||||
|
||||
this.logger.info({
|
||||
class: AuthController.name,
|
||||
method: this.login.name,
|
||||
user_login: req.user.userLogin,
|
||||
msg: 'User logged in.',
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(LoginAuthGuard)
|
||||
@UseGuards(JwtAccessGuard)
|
||||
@Post('logout')
|
||||
async logout(
|
||||
@Request() req,
|
||||
@ -81,7 +83,14 @@ export class AuthController {
|
||||
|
||||
response.clearCookie('Refresh');
|
||||
response.clearCookie('Authentication');
|
||||
|
||||
|
||||
this.logger.info({
|
||||
class: AuthController.name,
|
||||
method: this.logout.name,
|
||||
user_login: req.user.userLogin,
|
||||
msg: 'User logged off',
|
||||
});
|
||||
|
||||
return req.logout();
|
||||
}
|
||||
|
||||
@ -100,6 +109,11 @@ export class AuthController {
|
||||
secure: true,
|
||||
expires: new Date(data.exp),
|
||||
});
|
||||
this.logger.debug({
|
||||
class: AuthController.name,
|
||||
method: this.refresh.name,
|
||||
msg: 'Updated Authentication cookie for access token.',
|
||||
});
|
||||
|
||||
if (data.refresh_token != refresh_token) {
|
||||
response.cookie('Refresh', data.refresh_token, {
|
||||
@ -107,11 +121,21 @@ export class AuthController {
|
||||
secure: true,
|
||||
expires: new Date(data.refresh_exp),
|
||||
});
|
||||
this.logger.debug({
|
||||
class: AuthController.name,
|
||||
method: this.refresh.name,
|
||||
msg: 'Updated Refresh cookie for refresh token.',
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
this.logger.error({
|
||||
class: AuthController.name,
|
||||
method: this.refresh.name,
|
||||
msg: 'Failed to refresh tokens.',
|
||||
error: err,
|
||||
});
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'Something went wrong.',
|
||||
@ -131,16 +155,32 @@ export class AuthController {
|
||||
try {
|
||||
const { user_login, user_name, password } = body;
|
||||
user = await this.users.register(user_login.toLowerCase(), user_name, password, true);
|
||||
this.logger.info({
|
||||
class: AuthController.name,
|
||||
method: this.register.name,
|
||||
user_login: user.userLogin,
|
||||
msg: 'User registered',
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof QueryFailedError) {
|
||||
if (err.message.includes('duplicate key value violates unique constraint "users_user_login_key"')) {
|
||||
this.logger.warn({
|
||||
class: AuthController.name,
|
||||
method: this.register.name,
|
||||
msg: 'Failed to register due to duplicate userLogin.',
|
||||
});
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'Username already exist.',
|
||||
};
|
||||
}
|
||||
}
|
||||
console.log('AuthController', err);
|
||||
this.logger.error({
|
||||
class: AuthController.name,
|
||||
method: this.register.name,
|
||||
msg: 'Failed to register.',
|
||||
error: err,
|
||||
});
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'Something went wrong when creating user.',
|
||||
@ -156,7 +196,12 @@ export class AuthController {
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('AuthController', err);
|
||||
this.logger.error({
|
||||
class: AuthController.name,
|
||||
method: this.register.name,
|
||||
msg: 'Failed to login after registering.',
|
||||
error: err,
|
||||
});
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'Something went wrong while logging in.',
|
||||
@ -177,6 +222,6 @@ export class AuthController {
|
||||
|
||||
return {
|
||||
success: true,
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user