Skip to content

Commit bb1080a

Browse files
authored
Merge pull request #11 from Libertech-FR/authentication
Authentication
2 parents e7b69d8 + 7b8103e commit bb1080a

File tree

26 files changed

+1539
-1507
lines changed

26 files changed

+1539
-1507
lines changed

app/nuxt.config.ts

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { extensions } from '@libertech-fr/teaket_common'
22
import pugPlugin from 'vite-plugin-pug'
3-
import { resolve } from 'node:path'
3+
import openapiTS from 'openapi-typescript'
4+
import { writeFileSync } from 'fs'
5+
import { resolve } from 'path'
46

57
const TK_APP_API_URL = process.env.TK_APP_API_URL || 'http://localhost:7100'
6-
const TK_APP_AUTH_SECRET = process.env.TK_APP_AUTH_SECRET
78

89
// https://nuxt.com/docs/api/configuration/nuxt-config
910
export default defineNuxtConfig({
@@ -26,41 +27,49 @@ export default defineNuxtConfig({
2627
global: true,
2728
dirs: [{ path: '~/components', prefix: 'tk' }],
2829
},
29-
modules: ['nuxt-api-party', '@sidebase/nuxt-auth', 'nuxt-quasar-ui', '@vueuse/nuxt', 'dayjs-nuxt', ...extensions.appSetup.default()],
30-
dayjs: {
31-
locales: ['fr'],
32-
defaultLocale: 'fr',
33-
},
30+
modules: [
31+
'@nuxt-alt/auth',
32+
'@nuxt-alt/http',
33+
'@pinia/nuxt',
34+
'nuxt-quasar-ui',
35+
'@vueuse/nuxt',
36+
'dayjs-nuxt',
37+
...extensions.appSetup.default(),
38+
],
3439
auth: {
35-
baseURL: `${TK_APP_API_URL}/core/auth`,
36-
provider: {
37-
type: 'local',
38-
endpoints: {
39-
signIn: { path: '/login', method: 'post' },
40-
signOut: { path: '/logout', method: 'post' },
41-
signUp: { path: '/register', method: 'post' },
42-
getSession: { path: '/session', method: 'get' },
43-
},
44-
pages: {
45-
login: '/login',
46-
},
47-
},
48-
session: {
49-
enableRefreshOnWindowFocus: true,
50-
enableRefreshPeriodically: 5 * 60 * 1_000,
51-
},
52-
globalAppMiddleware: {
53-
isEnabled: true,
54-
},
55-
},
56-
runtimeConfig: {
57-
authJs: {
58-
secret: TK_APP_AUTH_SECRET,
59-
},
60-
public: {
61-
authJs: {
62-
// baseUrl: process.env.NUXT_NEXTAUTH_URL, // The URL of your deployed app (used for origin Check in production)
63-
verifyClientOnEveryRequest: true, // whether to hit the /auth/session endpoint on every client request
40+
globalMiddleware: true,
41+
rewriteRedirects: true,
42+
watchLoggedIn: true,
43+
strategies: {
44+
local: {
45+
scheme: 'refresh',
46+
token: {
47+
property: 'access_token',
48+
maxAge: 60 * 5,
49+
},
50+
refreshToken: {
51+
property: 'refresh_token',
52+
data: 'refresh_token',
53+
maxAge: 60 * 60 * 4,
54+
},
55+
user: {
56+
property: 'user',
57+
autoFetch: true,
58+
},
59+
clientId: false,
60+
grantType: false,
61+
endpoints: {
62+
login: { url: `${TK_APP_API_URL}/core/auth/local`, method: 'post', headers: { 'Content-Type': 'application/json' } },
63+
refresh: { url: `${TK_APP_API_URL}/core/auth/refresh`, method: 'post', headers: { 'Content-Type': 'application/json' } },
64+
logout: { url: `${TK_APP_API_URL}/core/auth/logout`, method: 'post' },
65+
user: { url: `${TK_APP_API_URL}/core/auth/session`, method: 'get', propertyName: 'user' },
66+
},
67+
redirect: {
68+
logout: '/login',
69+
login: '/',
70+
},
71+
tokenType: 'Bearer',
72+
autoRefresh: true,
6473
},
6574
},
6675
},
@@ -70,16 +79,6 @@ export default defineNuxtConfig({
7079
quasar: {
7180
iconSet: 'mdi-v5',
7281
},
73-
apiParty: {
74-
endpoints: {
75-
api: {
76-
url: TK_APP_API_URL,
77-
schema: `${TK_APP_API_URL}/swagger/json`,
78-
cookies: true,
79-
},
80-
},
81-
allowClient: true,
82-
},
8382
vite: {
8483
define: {
8584
'process.env.DEBUG': process.env.NODE_ENV === 'development',
@@ -100,4 +99,16 @@ export default defineNuxtConfig({
10099
// typeCheck: 'build,
101100
shim: false,
102101
},
102+
hooks: {
103+
'ready': async () => {
104+
console.log('[OpenapiTS] Generating .nuxt/types/service-api.d.ts...')
105+
try {
106+
const fileData = await openapiTS(`${TK_APP_API_URL}/swagger/json`)
107+
writeFileSync('.nuxt/types/service-api.d.ts', fileData)
108+
console.log('[OpenapiTS] Generated .nuxt/types/service-api.d.ts !')
109+
} catch (error) {
110+
console.debug('[OpenapiTS] Error while generating .nuxt/types/service-api.d.ts', error)
111+
}
112+
}
113+
}
103114
})

app/package.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,37 @@
66
"dev": "nuxt dev",
77
"generate": "nuxt generate",
88
"preview": "nuxt preview",
9-
"postinstall": "nuxt prepare"
9+
"postinstall": "nuxt prepare",
10+
"generate:api": "openapi-typescript http://localhost:7100/swagger/json -o ./.nuxt/types/service-api.d.ts",
11+
"upgrade:packages": "npx npm-check-updates -u --reject @types/node"
1012
},
1113
"dependencies": {
12-
"@auth/core": "^0.12.0",
1314
"@libertech-fr/teaket_common": "*",
15+
"@nuxt-alt/auth": "^2.6.1",
16+
"@nuxt-alt/http": "^1.6.8",
17+
"@pinia/nuxt": "^0.4.11",
1418
"@quasar/extras": "^1.16.6",
1519
"@vueuse/router": "^10.4.1",
1620
"cookie": "^0.5.0",
17-
"quasar": "^2.12.5"
21+
"pinia": "^2.1.6",
22+
"quasar": "^2.12.6"
1823
},
1924
"devDependencies": {
2025
"@nuxt/devtools": "latest",
21-
"@sidebase/nuxt-auth": "^0.6.0-beta.5",
2226
"@types/node": "^18.17.3",
2327
"@vueuse/core": "^10.4.1",
2428
"@vueuse/nuxt": "^10.4.1",
2529
"dayjs-nuxt": "^1.1.2",
26-
"nuxt": "^3.6.5",
27-
"nuxt-api-party": "^0.17.0",
30+
"@typescript-eslint/eslint-plugin": "^6.6.0",
31+
"@typescript-eslint/parser": "^6.6.0",
32+
"eslint": "^8.49.0",
33+
"nuxt": "~3.6.5",
2834
"nuxt-quasar-ui": "^2.0.5",
29-
"openapi-typescript": "^6.5.4",
35+
"openapi-typescript": "^6.5.5",
36+
"prettier": "^3.0.3",
3037
"vite-plugin-pug": "^0.3.2"
38+
},
39+
"resolutions": {
40+
"vite": "4.3.9"
3141
}
3242
}

app/src/app.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<NuxtLayout>
3-
<NuxtPage />
4-
</NuxtLayout>
5-
</template>
2+
<NuxtLayout>
3+
<NuxtPage />
4+
</NuxtLayout>
5+
</template>

app/src/composables/useApiFetch.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

app/src/composables/useHttpApi.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// noinspection JSUnusedGlobalSymbols
2+
3+
// @ts-ignore
4+
import { paths } from '@/../.nuxt/types/service-api'
5+
6+
import { Ref } from 'vue'
7+
import { FetchError } from 'ofetch/dist/node'
8+
import { AvailableRouterMethod, NitroFetchRequest } from 'nitropack'
9+
import { AsyncData, FetchResult, UseFetchOptions } from 'nuxt/app'
10+
import { KeysOf } from 'nuxt/dist/app/composables/asyncData'
11+
12+
export type MaybeRef2<T> = T | Ref<T>
13+
export type MaybeRefOrGetter2<T> = MaybeRef2<T> | (() => T)
14+
15+
export type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207 | '2XX' | 'default'
16+
export type ErrorStatus =
17+
500
18+
| '5XX'
19+
| 400
20+
| 401
21+
| 402
22+
| 403
23+
| 404
24+
| 405
25+
| 406
26+
| 407
27+
| 408
28+
| 409
29+
| 410
30+
| 411
31+
| 412
32+
| 413
33+
| 414
34+
| 415
35+
| 416
36+
| 417
37+
| 418
38+
| 420
39+
| 421
40+
| 422
41+
| 423
42+
| 424
43+
| 425
44+
| 426
45+
| 429
46+
| 431
47+
| 444
48+
| 450
49+
| 451
50+
| 497
51+
| 498
52+
| 499
53+
| '4XX'
54+
export type HttpMethod = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace'
55+
56+
export interface Paths extends paths {
57+
[key: string]: any
58+
}
59+
60+
type MediaTypes<T, Status extends keyof any> = {
61+
[S in Status]: T extends {
62+
responses: {
63+
[_ in S]: {
64+
content: {
65+
'application/json': infer Model
66+
}
67+
}
68+
}
69+
} ? Model : never
70+
}[Status]
71+
export type OpenApiResponse<T> = MediaTypes<T, OkStatus>
72+
export type OpenApiError<T> = MediaTypes<T, ErrorStatus>
73+
74+
export type BodyType<ResT extends keyof Paths & string, Method extends HttpMethod> = Paths[ResT][Lowercase<Method>]['requestBody']['content']['application/json']
75+
export type ResponseType<ResT extends keyof Paths & string, Method extends HttpMethod> = OpenApiResponse<Paths[ResT][Lowercase<Method>]>
76+
77+
export function useHttpApi<
78+
ResT extends keyof Paths & string,
79+
ErrorT = FetchError,
80+
ReqT extends NitroFetchRequest = NitroFetchRequest,
81+
Method extends AvailableRouterMethod<ReqT> = ResT extends void ? 'get' extends AvailableRouterMethod<ReqT> ? 'get' : AvailableRouterMethod<ReqT> : AvailableRouterMethod<ReqT>,
82+
_ResT = ResT extends void ? FetchResult<ReqT, Method> : ResT, DataT = _ResT,
83+
PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = null
84+
>(
85+
path: MaybeRefOrGetter2<ResT>,
86+
opts?: UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method> & {
87+
method: Method,
88+
body?: Paths[ResT][Lowercase<Method>]['requestBody']['content']['application/json'],
89+
},
90+
): AsyncData<OpenApiResponse<Paths[ResT][Lowercase<Method>]> | undefined, FetchError<OpenApiError<Paths[ResT][Lowercase<Method>]>>> {
91+
// ): Promise<AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | null>> {
92+
93+
return useHttp(path, {
94+
baseURL: 'http://localhost:7100',
95+
...opts,
96+
})
97+
}

app/src/layouts/default.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
div
33
tk-appbar
44
slot
5-
</template>
5+
</template>

app/src/pages/index.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ div
44
tk-hook(name="accueil" :data='{exemple: 1}' debug)
55
span Accueil
66
tk-hook(name="index.accueil.bottom")
7-
q-btn(@click='signIn()') signIn
8-
q-btn(@click='signOut()') signOut
7+
q-btn(@click='logout') signOut
98
</template>
109

1110
<script lang='ts' setup>
12-
import { definePageMeta, useAuth } from '#imports'
13-
const { signIn, signOut, session, status, cookies, getProviders, user, sessionToken } = useAuth()
11+
const logout = () => {
12+
useAuth().logout()
13+
}
1414
</script>

0 commit comments

Comments
 (0)