Skip to content

Commit ef69bd6

Browse files
authored
Merge pull request #12 from Libertech-FR/authentication
Authentication
2 parents 3eaa4d1 + d475193 commit ef69bd6

File tree

14 files changed

+207
-56
lines changed

14 files changed

+207
-56
lines changed

app/nuxt.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export default defineNuxtConfig({
4545
scheme: 'refresh',
4646
token: {
4747
property: 'access_token',
48-
maxAge: 60 * 5,
48+
maxAge: 1 * 5,
49+
// maxAge: 60 * 5,
4950
},
5051
refreshToken: {
5152
property: 'refresh_token',
Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
1-
<template lang="pug">
2-
div
3-
q-btn(v-for="button in buttons" :key="button.icon" round flat :icon="`mdi-${button.icon}`" size="lg")
4-
q-tooltip.text-body2(transition-show="scale" transition-hide="scale") {{ button.name }}
1+
<template lang='pug'>
2+
div
3+
q-btn(v-for="button in buttons" :key="button.icon" round flat :icon="button.icon" size="lg")
4+
q-tooltip.text-body2(transition-show="scale" transition-hide="scale") {{ button.name }}
55
q-btn-dropdown(icon="mdi-account-circle-outline" round flat size="lg")
6-
q-list
7-
q-item(v-for="button in buttons" :key="button.name")
8-
q-btn(flat :icon="`mdi-${button.icon}`" :label="button.name" color="primary")
6+
q-list
7+
q-item.q-pa-none(v-for="button in buttons" :key="button.name")
8+
q-btn.full-width.items-baseline.q-pa-sm(
9+
:icon="button.icon"
10+
:label="button.name"
11+
:color="button?.color || 'primary'"
12+
@click="button?.action"
13+
:to='button?.to'
14+
flat
15+
dense
16+
)
917
</template>
1018

11-
<script lang="ts" setup>
19+
<script lang='ts' setup>
1220
const buttons = [
13-
{ icon: 'cog', name: 'Paramètres', action: '' },
14-
{ icon: 'bell', name: 'Notifications', action: '' },
15-
{ icon: 'help', name: 'Aide', action: '' },
16-
21+
{
22+
icon: 'mdi-cog',
23+
name: 'Paramètres',
24+
to: '/settings',
25+
},
26+
{
27+
icon: 'mdi-bell',
28+
name: 'Notifications',
29+
to: '#',
30+
},
31+
{
32+
icon: 'mdi-help',
33+
name: 'Aide',
34+
to: '#',
35+
},
36+
{
37+
icon: 'mdi-logout',
38+
name: 'Déconnexion',
39+
color: 'negative',
40+
action: async () => {
41+
await useAuth().logout()
42+
useRouter().go(0)
43+
},
44+
},
1745
]
18-
</script>
46+
</script>

app/src/composables/useHttpApi.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export function useHttpApi<
8989
},
9090
): AsyncData<OpenApiResponse<Paths[ResT][Lowercase<Method>]> | undefined, FetchError<OpenApiError<Paths[ResT][Lowercase<Method>]>>> {
9191
// ): Promise<AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | null>> {
92-
9392
return useHttp(path, {
9493
baseURL: 'http://localhost:7100',
9594
...opts,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template lang='pug'>
2+
q-layout(view="lHh Lpr fff")
3+
q-page-container
4+
q-page.window-height.window-width.row.justify-center.items-center(
5+
:style='{background: "linear-gradient(#8274C5, #5A4A9F)"}'
6+
)
7+
nuxt-page
8+
</template>

app/src/pages/index.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
21
<template lang='pug'>
32
div
43
tk-hook(name="accueil" :data='{exemple: 1}' debug)
54
span Accueil
65
tk-hook(name="index.accueil.bottom")
7-
q-btn(@click='logout') signOut
6+
q-btn(@click="call") Call
87
</template>
98

109
<script lang='ts' setup>
11-
const logout = () => {
12-
useAuth().logout()
10+
const call = () => {
11+
const { data } = useHttpApi('/tickets/ticket')
1312
}
1413
</script>

app/src/pages/login.vue

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1-
<template>
2-
<div>
3-
<h1>Login Page</h1>
4-
<form @submit.prevent="submit">
5-
<input v-model="formData.username" type="text" placeholder="Username">
6-
<input v-model="formData.password" type="password" placeholder="Password">
7-
<button @click='submit'>
8-
sign in
9-
</button>
10-
</form>
11-
</div>
1+
<template lang='pug'>
2+
q-card.column.shadow-24(:style='{width: "250px"}')
3+
q-toolbar.bg-primary.text-white
4+
q-toolbar-title Connexion
5+
form(@submit.prevent='submit')
6+
q-card.no-shadow
7+
q-card-section.q-pt-sm.q-px-lg.q-pb-lg
8+
q-input(v-model="formData.username" label="Username" type="text")
9+
q-input(v-model="formData.password" label="Password" type="password" auto-complete='current-password')
10+
q-inner-loading(:showing='pending')
11+
q-spinner-grid(color='primary' size='50px')
12+
q-card-actions
13+
q-space
14+
q-btn(@click.prevent='submit' type='submit' color='primary') Se connecter
15+
q-space
1216
</template>
1317

1418
<script lang="ts" setup>
1519
import { ref } from 'vue'
1620
1721
definePageMeta({
22+
layout: 'simple-centered',
1823
auth: {
1924
unauthenticatedOnly: true,
2025
navigateAuthenticatedTo: '/'
2126
}
2227
})
2328
24-
// const { signIn, token, data, status, lastRefreshedAt } = useAuth()
25-
29+
const pending = ref(false)
2630
const formData = ref({
2731
username: '',
2832
password: '',
2933
})
3034
3135
const submit = async () => {
32-
console.log("submitting", useAuth().getStrategy())
36+
pending.value = true
3337
await useAuth().loginWith('local', {
3438
body: formData.value,
3539
})
40+
pending.value = false
3641
}
3742
</script>

service/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ lerna-debug.log*
3737
.env
3838
.env.*
3939
!.env.example
40+
.dev-token.json

service/src/_common/abstracts/dto/metadata.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IsObject, ValidateNested, IsOptional } from 'class-validator'
1+
import { IsObject, IsOptional, ValidateNested } from 'class-validator'
22
import { ApiProperty } from '@nestjs/swagger'
33
import { Type } from 'class-transformer'
44
import { MetadataPartDto } from './parts/metadata.part.dto'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { IdentityType } from '~/_common/types/identity.type'
2+
import { Types } from 'mongoose'
3+
4+
export class ConsoleSession {
5+
public readonly _id: string = '000000000000000000000000'
6+
public readonly username: string = 'console'
7+
public readonly entityId: Types.ObjectId = new Types.ObjectId('000000000000000000000000')
8+
public readonly displayName: string = 'Console'
9+
public constructor() {
10+
}
11+
}

service/src/core/auth/auth.controller.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, HttpStatus, Post, Res, UseGuards } from '@nestjs/common'
1+
import { Body, Controller, Get, Header, Headers, HttpStatus, Post, Res, UseGuards } from '@nestjs/common'
22
import { AuthService } from './auth.service'
33
import { AbstractController } from '~/_common/abstracts/abstract.controller'
44
import { Response } from 'express'
@@ -23,7 +23,7 @@ export class AuthController extends AbstractController {
2323
@Post('local')
2424
@UseGuards(AuthGuard('local'))
2525
public async authenticateWithLocal(@Res() res: Response, @ReqIdentity() user: IdentityType): Promise<Response> {
26-
const tokens = await this.service.createToken(user)
26+
const tokens = await this.service.createTokens(user)
2727
return res.status(HttpStatus.OK).json({
2828
...tokens,
2929
user,
@@ -32,16 +32,24 @@ export class AuthController extends AbstractController {
3232

3333
@Get('session')
3434
@UseGuards(AuthGuard('jwt'))
35-
public async session(@Res() res: Response, @ReqIdentity() user: IdentityType): Promise<Response> {
36-
const tokens = await this.service.createToken(user)
35+
public async session(@Res() res: Response, @ReqIdentity() identity: IdentityType): Promise<Response> {
36+
const user = await this.service.getSessionData(identity)
3737
return res.status(HttpStatus.OK).json({
38-
...tokens,
3938
user,
4039
})
4140
}
4241

42+
@Post('refresh')
43+
public async refresh(@Res() res: Response, @Body() body: any): Promise<Response> {
44+
const tokens = await this.service.renewTokens(body.refresh_token)
45+
return res.status(HttpStatus.OK).json({
46+
...tokens,
47+
})
48+
}
49+
4350
@Post('logout')
44-
public async logout(@Res() res: Response): Promise<Response> {
51+
public async logout(@Res() res: Response, @Headers('Authorization') jwt: string): Promise<Response> {
52+
await this.service.clearSession(jwt.replace(/^Bearer\s/, ''))
4553
return res.status(HttpStatus.OK).send()
4654
}
4755
}

0 commit comments

Comments
 (0)