-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move token endpoint into token controller
- Loading branch information
1 parent
5b43227
commit b7447b3
Showing
7 changed files
with
84 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { HttpContext } from "@adonisjs/core/http"; | ||
|
||
import RefreshTokenValidator from "#services/auth/token/refresh/refresh-token.validator"; | ||
import TokenHandler from "#services/auth/token/token.handler"; | ||
import BlResponseHandler from "#services/response/bl-response.handler"; | ||
import { RefreshToken } from "#services/types/refresh-token"; | ||
import { BlError } from "#shared/bl-error/bl-error"; | ||
import { BlapiResponse } from "#shared/blapi-response/blapi-response"; | ||
import { tokenValidator } from "#validators/token"; | ||
|
||
export default class TokensController { | ||
async token(ctx: HttpContext) { | ||
const { refreshToken } = await ctx.request.validateUsing(tokenValidator); | ||
RefreshTokenValidator.validate(refreshToken).then( | ||
// @ts-expect-error fixme: auto ignored | ||
(validatedRefreshToken: RefreshToken) => { | ||
TokenHandler.createTokens(validatedRefreshToken.username).then( | ||
(jwTokens: { accessToken: string; refreshToken: string }) => { | ||
BlResponseHandler.sendResponse( | ||
ctx, | ||
new BlapiResponse([ | ||
{ accessToken: jwTokens.accessToken }, | ||
{ refreshToken: jwTokens.refreshToken }, | ||
]), | ||
); | ||
}, | ||
(createTokenError: BlError) => { | ||
BlResponseHandler.sendErrorResponse( | ||
ctx, | ||
new BlError("could not create tokens") | ||
.store("oldRefreshToken", refreshToken) | ||
.code(906) | ||
.add(createTokenError), | ||
); | ||
}, | ||
); | ||
}, | ||
(refreshTokenValidationError: BlError) => { | ||
BlResponseHandler.sendErrorResponse( | ||
ctx, | ||
new BlError("refreshToken not valid") | ||
.code(909) | ||
.add(refreshTokenValidationError), | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import vine from "@vinejs/vine"; | ||
|
||
export const tokenValidator = vine.compile( | ||
vine.object({ | ||
refreshToken: vine.string().jwt(), | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters