-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add cookie authentication #45
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import httpStatus from 'http-status'; | ||
import { Body, Controller, Post, Request, Route, Security } from 'tsoa'; | ||
|
||
import { AuthService } from 'services/auth'; | ||
import { | ||
CreateUserParams, | ||
|
@@ -8,27 +9,43 @@ import { | |
AuthenticatedRequest, | ||
LoginParams, | ||
} from 'types'; | ||
import { COOKIE_NAME, cookieConfig } from 'utils/auth'; | ||
|
||
@Route('v1/auth') | ||
export class AuthControllerV1 extends Controller { | ||
@Post('/register') | ||
public async register(@Body() user: CreateUserParams): Promise<ReturnAuth> { | ||
const authReturn = await AuthService.register(user); | ||
public async register( | ||
@Body() user: CreateUserParams, | ||
@Request() req: AuthenticatedRequest, | ||
): Promise<ReturnAuth | null> { | ||
const { sessionId, ...authReturn } = await AuthService.register(user); | ||
|
||
const { res } = req; | ||
res?.cookie(COOKIE_NAME, sessionId, cookieConfig); | ||
|
||
this.setStatus(httpStatus.CREATED); | ||
return authReturn; | ||
} | ||
|
||
@Post('/login') | ||
public async login(@Body() loginParams: LoginParams): Promise<ReturnAuth> { | ||
const authReturn = await AuthService.login(loginParams); | ||
public async login( | ||
@Body() loginParams: LoginParams, | ||
@Request() req: AuthenticatedRequest, | ||
): Promise<ReturnAuth | null> { | ||
const { sessionId, ...authReturn } = await AuthService.login(loginParams); | ||
const { res } = req; | ||
res?.cookie(COOKIE_NAME, sessionId, cookieConfig); | ||
this.setStatus(httpStatus.OK); | ||
return authReturn; | ||
} | ||
|
||
@Post('/logout') | ||
@Security('cookie') | ||
@Security('jwt') | ||
public async logout(@Request() req: AuthenticatedRequest): Promise<void> { | ||
await AuthService.logout(req.user.token); | ||
const { res } = req; | ||
res?.clearCookie(COOKIE_NAME); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment |
||
this.setStatus(httpStatus.OK); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,13 @@ import { | |
PasswordResetCodeRequest, | ||
ResetPassword, | ||
} from 'types'; | ||
import { COOKIE_NAME } from 'utils/auth'; | ||
|
||
@Route('v1/users') | ||
export class UsersControllerV1 extends Controller { | ||
@Get() | ||
@Security('jwt') | ||
@Security('cookie') | ||
public async index(): Promise<ReturnUser[]> { | ||
const users = await UserService.all(); | ||
this.setStatus(httpStatus.OK); | ||
|
@@ -32,6 +34,7 @@ export class UsersControllerV1 extends Controller { | |
|
||
@Get('/me') | ||
@Security('jwt') | ||
@Security('cookie') | ||
public async getMe( | ||
@Request() req: AuthenticatedRequest, | ||
): Promise<ReturnUser | null> { | ||
|
@@ -42,6 +45,7 @@ export class UsersControllerV1 extends Controller { | |
|
||
@Get('{id}') | ||
@Security('jwt') | ||
@Security('cookie') | ||
public async find(@Path() id: string): Promise<ReturnUser | null> { | ||
const user = await UserService.find(id); | ||
this.setStatus(httpStatus.OK); | ||
|
@@ -50,6 +54,7 @@ export class UsersControllerV1 extends Controller { | |
|
||
@Put('{id}') | ||
@Security('jwt') | ||
@Security('cookie') | ||
public async update( | ||
@Path() id: string, | ||
@Body() requestBody: UpdateUserParams, | ||
|
@@ -61,8 +66,14 @@ export class UsersControllerV1 extends Controller { | |
|
||
@Delete('{id}') | ||
@Security('jwt') | ||
public async destroy(@Path() id: string): Promise<void> { | ||
@Security('cookie') | ||
public async destroy( | ||
@Path() id: string, | ||
@Request() req: AuthenticatedRequest, | ||
): Promise<void> { | ||
const { user, res } = req; | ||
await UserService.destroy(id); | ||
if (user.id === id) res?.clearCookie(COOKIE_NAME); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this line is needed. You might want to destroy all sessions of the user though. |
||
this.setStatus(httpStatus.NO_CONTENT); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this only be done if the authentication scheme is cookies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to differentiate between cases for settings and clearing cookies, I'd rather have different endpoints for cookies and tokens than having the user tell me what method it's using, what do you think about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you know what method the client is using? Meaning there is either a token or a cookie.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of a register or login endpoint we don't know what method will be used, in other cases we might me able to deduce it based on the authentication method, but in this case we don't know , maybe I'm overlooking something. @chaba11 do you have any idea regarding this?