mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 12:33:22 +00:00
feat: add get test username page
This commit is contained in:
@@ -16,7 +16,6 @@ const cookieExtractor = function (req: Request) {
|
|||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class JwtRefreshStrategy extends PassportStrategy(Strategy, 'jwt-refresh') {
|
export class JwtRefreshStrategy extends PassportStrategy(Strategy, 'jwt-refresh') {
|
||||||
constructor(
|
constructor(
|
||||||
|
|||||||
@@ -25,12 +25,15 @@ export class RefreshTokenHandler implements ICommandHandler<RefreshTokenCommand>
|
|||||||
userId,
|
userId,
|
||||||
date: new Date(),
|
date: new Date(),
|
||||||
}
|
}
|
||||||
|
|
||||||
const accessToken = jwt.sign(payload, accessSecretKey, {
|
const accessToken = jwt.sign(payload, accessSecretKey, {
|
||||||
expiresIn: shortAccessToken ? '10s' : '10m',
|
expiresIn: shortAccessToken ? '10s' : '10m',
|
||||||
})
|
})
|
||||||
|
|
||||||
const refreshToken = jwt.sign(payload, refreshSecretKey, {
|
const refreshToken = jwt.sign(payload, refreshSecretKey, {
|
||||||
expiresIn: '30d',
|
expiresIn: '30d',
|
||||||
})
|
})
|
||||||
|
|
||||||
const expiresIn = addDays(new Date(), 30)
|
const expiresIn = addDays(new Date(), 30)
|
||||||
|
|
||||||
await this.authRepository.createRefreshToken(userId, refreshToken, expiresIn)
|
await this.authRepository.createRefreshToken(userId, refreshToken, expiresIn)
|
||||||
|
|||||||
@@ -42,6 +42,15 @@ export class UsersController {
|
|||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('/test-user-name')
|
||||||
|
async testUserNamePage() {
|
||||||
|
const user = await this.usersService.getUserByEmail('example@google.com')
|
||||||
|
|
||||||
|
if (!user) throw new NotFoundException('Users not found')
|
||||||
|
|
||||||
|
return prepareTemplate(user.name)
|
||||||
|
}
|
||||||
|
|
||||||
@UseGuards(BaseAuthGuard)
|
@UseGuards(BaseAuthGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async create(@Body() createUserDto: CreateUserDto) {
|
async create(@Body() createUserDto: CreateUserDto) {
|
||||||
@@ -66,3 +75,56 @@ export class UsersController {
|
|||||||
return await this.usersService.deleteAllUsers()
|
return await this.usersService.deleteAllUsers()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const template = `<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport"
|
||||||
|
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<style>
|
||||||
|
*, *::before, *::after {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
:root {
|
||||||
|
color-scheme: light dark;
|
||||||
|
font-family: sans-serif;
|
||||||
|
}
|
||||||
|
main {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap:1rem;
|
||||||
|
height: 100svh;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
<div>
|
||||||
|
Current test account username is <strong>{{testUserName}}</strong>
|
||||||
|
</div>
|
||||||
|
<button id="copy-test-user-name">Copy to clipboard</button>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
<script>
|
||||||
|
const button = document.getElementById('copy-test-user-name')
|
||||||
|
button.addEventListener('click', () => {
|
||||||
|
navigator.clipboard.writeText("{{testUserName}}").then(() =>{
|
||||||
|
button.innerText = 'Copied!'
|
||||||
|
setTimeout(() => {
|
||||||
|
button.innerText = 'Copy to clipboard'
|
||||||
|
}, 2000)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
</html>`
|
||||||
|
|
||||||
|
function prepareTemplate(username: string) {
|
||||||
|
return template.replaceAll('{{testUserName}}', username)
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ export class UsersService {
|
|||||||
return await this.usersRepository.findUserById(id)
|
return await this.usersRepository.findUserById(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getUserByEmail(email: string) {
|
||||||
|
return await this.usersRepository.findUserByEmail(email)
|
||||||
|
}
|
||||||
|
|
||||||
async deleteUserById(id: string): Promise<boolean> {
|
async deleteUserById(id: string): Promise<boolean> {
|
||||||
return await this.usersRepository.deleteUserById(id)
|
return await this.usersRepository.deleteUserById(id)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user