open-music-api-node
Version:
68 lines (54 loc) • 2.12 kB
JavaScript
class AuthHandler {
constructor(authService, usersService, tokenManager, validator) {
this._authService = authService;
this._usersService = usersService;
this._tokenManager = tokenManager;
this._validator = validator;
this.postAuthenticationHandler = this.postAuthenticationHandler.bind(this);
this.putAuthenticationHandler = this.putAuthenticationHandler.bind(this);
this.deleteAuthenticationHandler = this.deleteAuthenticationHandler.bind(this);
}
async postAuthenticationHandler(request, h) {
this._validator.validatePostAuthenticationPayload(request.payload);
const { username, password } = request.payload;
const id = await this._usersService.verifyUserCredential(username, password);
const accessToken = this._tokenManager.generateAccessToken({ id });
const refreshToken = this._tokenManager.generateRefreshToken({ id });
await this._authService.addRefreshToken(refreshToken);
const response = h.response({
status: 'success',
message: 'Authentication berhasil ditambahkan',
data: {
accessToken,
refreshToken,
},
});
response.code(201);
return response;
}
async putAuthenticationHandler(request) {
this._validator.validatePutAuthenticationPayload(request.payload);
const { refreshToken } = request.payload;
await this._authService.verifyRefreshToken(refreshToken);
const { id } = this._tokenManager.verifyRefreshToken(refreshToken);
const accessToken = this._tokenManager.generateAccessToken({ id });
return {
status: 'success',
message: 'Access Token berhasil diperbarui',
data: {
accessToken,
},
};
}
async deleteAuthenticationHandler(request) {
this._validator.validateDeleteAuthenticationPayload(request.payload);
const { refreshToken } = request.payload;
await this._authService.verifyRefreshToken(refreshToken);
await this._authService.deleteRefreshToken(refreshToken);
return {
status: 'success',
message: 'Refresh token berhasil dihapus',
};
}
}
module.exports = AuthHandler;