-
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.
[WIP] login したユーザーは自分の情報にのみアクセスできる (#32)
* yarn nest generate guard (user) * [backend] Implement UserGuard and add it to UserController * user guard don't user UserService class
- Loading branch information
Showing
3 changed files
with
27 additions
and
0 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,7 @@ | ||
import { UserGuard } from './user.guard'; | ||
|
||
describe('UserGuard', () => { | ||
it('should be defined', () => { | ||
expect(new UserGuard()).toBeDefined(); | ||
}); | ||
}); |
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,16 @@ | ||
import { CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
|
||
export class UserGuard implements CanActivate { | ||
|
||
canActivate( | ||
context: ExecutionContext, | ||
): boolean | Promise<boolean> | Observable<boolean> { | ||
const req = context.switchToHttp().getRequest(); | ||
const { params, user } = req; | ||
if (params?.id == null) { | ||
return true; | ||
} | ||
return user.id === Number(params.id); | ||
} | ||
} |