File tree Expand file tree Collapse file tree 3 files changed +22
-7
lines changed Expand file tree Collapse file tree 3 files changed +22
-7
lines changed Original file line number Diff line number Diff line change 2929 "type" : " module" ,
3030 "dependencies" : {
3131 "@vincjo/datatables" : " ^1.6.0" ,
32+ "jose" : " ^4.14.4" ,
3233 "sweetalert2" : " ^11.4.37"
3334 }
3435}
Original file line number Diff line number Diff line change 1- import { redirect } from '@sveltejs/kit'
1+ import { fail , redirect } from '@sveltejs/kit'
2+ import * as jose from 'jose'
23import { userAuthentication } from '$lib/server/api/v1/auth_api'
34
45export const actions = {
@@ -9,11 +10,22 @@ export const actions = {
910
1011 // Get form data
1112 const formData = await request . formData ( )
12- const authData = await userAuthentication ( fetch , formData )
13+ // Set auth data
14+ let authData
15+ try {
16+ authData = await userAuthentication ( fetch , formData )
17+ } catch ( error ) {
18+ console . error ( error )
19+ return fail ( 400 , { invalidMessage : 'Invalid credentials' , invalid : true } )
20+ }
21+ const authToken = authData . access_token
22+ // Decode JWT token claims
23+ const tokenClaims = jose . decodeJwt ( authToken )
1324
25+ // Set the authentication cookie
1426 cookies . set ( 'fastapiusersauth' , authData . access_token , {
1527 path : '/' ,
16- maxAge : 60 * 60 * 24 * 7 , // 1 week
28+ expires : new Date ( tokenClaims . exp * 1000 ) ,
1729 sameSite : 'lax' ,
1830 secure : true ,
1931 httpOnly : true
Original file line number Diff line number Diff line change 11<script >
2- import { goto } from ' $app/navigation'
3- import { userStore } from ' $lib/stores/authStores'
4-
2+ export let form
53 let loginError = false
64
5+ if (form? .invalid ) {
6+ loginError = true
7+ }
8+
79< / script>
810
911< div class = " container" >
1820 < input name= " username" type= " email" class = " form-control { loginError ? 'is-invalid' : '' }" id= " userEmail" aria- describedby= " emailHelp" required>
1921 < div id= " emailHelp" class = " form-text" > The email you provided to the IT manager< / div>
2022 < div class = " invalid-feedback" >
21- Can not perform login with the data provided
23+ { form ? . invalidMessage }
2224 < / div>
2325 < / div>
2426 < div class = " mb-3" >
You can’t perform that action at this time.
0 commit comments