Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/src/core/api/auth/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ class Controller {
}

refreshToken = async req => {
const data = await this.service.refreshToken(RefreshTokenDto(req.body), req.user.payload.id);
const data = await this.service.refreshToken(RefreshTokenDto(req.body));
return ValidHttpResponse.toOkResponse(data);
}


logout = async req => {
const data = await this.service.logout(LogoutDto(req.body));
return ValidHttpResponse.toOkResponse(data);
Expand Down
3 changes: 1 addition & 2 deletions backend/src/core/api/auth/auth.resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ export const AuthResolver = Module.builder()
route: '/refresh-token',
method: 'post',
interceptors: [RefreshTokenInterceptor],
middleware: [authMiddleware],
body: 'RefreshTokenDto',
controller: AuthController.refreshToken,
preAuthorization: true,
},

{
route: '/logout',
method: 'post',
Expand Down
6 changes: 6 additions & 0 deletions backend/src/core/modules/auth/auth.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ const PROFILE_SELECT = {
subscriptions: {
select: { plan_name: true },
},
roles: {
select: { name: true },
},
banned_by: true,
is_email_confirmed: true,
created_at: true,
};


const ROLE_SELECT = {
roles: {
select: { name: true },
Expand Down
12 changes: 11 additions & 1 deletion backend/src/core/modules/auth/service/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class Service {
};
}

async refreshToken(refreshTokenDto, userId) {
async refreshToken(refreshTokenDto) {
const result = await this.repository.findRefreshToken(refreshTokenDto.refresh_token);
if (!result) {
throw new UnAuthorizedException('Invalid or expired refresh token');
Expand All @@ -247,6 +247,7 @@ class Service {
throw new UnAuthorizedException('Invalid or expired refresh token');
}

const userId = result.user_id;
const roleNames = await this.#getRoleNames(userId);
const { accessToken, refreshToken } = await this.#issueTokens(userId, roleNames);

Expand Down Expand Up @@ -316,6 +317,13 @@ class Service {
}

#mapProfile(user) {
let status = 'ACTIVE';
if (user.banned_by) {
status = 'BANNED';
} else if (!user.is_email_confirmed) {
status = 'INACTIVE';
}

return {
id: user.id,
avatarUrl: user.avatar_url ?? null,
Expand All @@ -324,6 +332,8 @@ class Service {
phone: user.phone ?? null,
dateOfBirth: user.date_of_birth ?? null,
location: user.location ?? null,
roleName: user.roles?.name ? user.roles.name.toUpperCase() : null,
status,
subscriptions: user.subscriptions
? { planName: user.subscriptions.plan_name ?? null }
: undefined,
Expand Down
Loading