sveltekit-auth-example 1.0.18 → 1.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -1
- package/package.json +2 -3
- package/src/hooks.ts +7 -12
- package/src/routes/admin/+page.server.ts +3 -2
- package/src/routes/auth/[slug]/+server.ts +2 -1
- package/src/routes/auth/google/+server.ts +5 -6
- package/src/routes/profile/+page.server.ts +1 -1
- package/src/routes/teachers/+page.server.ts +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# Backlog
|
|
2
2
|
* Add username and Avatar icon to menu bar
|
|
3
|
-
* [Possible Bug] Getting HTTP 401 on https://play.google.com/log?format=json&hasfast=true&authuser=0 from google-auth-library. As I didn't explicitly request logging, it could be that Safari is preventing Google from further invading our privacy. Will require some investigation. The site works regardless.
|
|
4
3
|
* Consider not setting defaultUser in loginSession as it would simplify +layout.svelte.
|
|
5
4
|
* Refactor $env/dynamic/private and public
|
|
6
5
|
* Add password complexity checking on /register and /profile pages (only checks for length currently despite what the pages say)
|
|
7
6
|
|
|
7
|
+
# 1.0.19
|
|
8
|
+
* Added SvelteKit's cookies implementation in RequestEvent
|
|
9
|
+
* [Bug] Logout then go to http://localhost/admin gives error on auth.ts:39
|
|
10
|
+
|
|
8
11
|
# 1.0.18
|
|
9
12
|
* Bump dependencies
|
|
10
13
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sveltekit-auth-example",
|
|
3
3
|
"description": "SvelteKit Authentication Example",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.19",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Nate Stuyvesant",
|
|
7
7
|
"license": "https://github.com/nstuyvesant/sveltekit-auth-example/blob/master/LICENSE",
|
|
@@ -38,7 +38,6 @@
|
|
|
38
38
|
},
|
|
39
39
|
"type": "module",
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"cookie": "^0.5.0",
|
|
42
41
|
"dotenv": "^16.0.2",
|
|
43
42
|
"google-auth-library": "^8.5.1",
|
|
44
43
|
"jsonwebtoken": "^8.5.1",
|
|
@@ -54,7 +53,7 @@
|
|
|
54
53
|
"@types/pg": "^8.6.5",
|
|
55
54
|
"@typescript-eslint/eslint-plugin": "^5.36.1",
|
|
56
55
|
"@typescript-eslint/parser": "^5.36.1",
|
|
57
|
-
"bootstrap": "^5.2.
|
|
56
|
+
"bootstrap": "^5.2.1",
|
|
58
57
|
"bootstrap-icons": "^1.9.1",
|
|
59
58
|
"eslint": "^8.23.0",
|
|
60
59
|
"eslint-config-prettier": "^8.5.0",
|
package/src/hooks.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import * as cookie from 'cookie'
|
|
2
1
|
import type { Handle, RequestEvent } from '@sveltejs/kit'
|
|
3
2
|
import { query } from './routes/_db'
|
|
4
3
|
|
|
5
4
|
// Attach authorization to each server request (role may have changed)
|
|
6
|
-
async function
|
|
5
|
+
async function attachUserToRequestEvent(sessionId: string, event: RequestEvent) {
|
|
7
6
|
const sql = `
|
|
8
7
|
SELECT * FROM get_session($1);`
|
|
9
8
|
const { rows } = await query(sql, [sessionId])
|
|
@@ -12,24 +11,20 @@ async function attachUserToRequest(sessionId: string, event: RequestEvent) {
|
|
|
12
11
|
}
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
function deleteCookieIfNoUser(event: RequestEvent, response: Response) {
|
|
16
|
-
if (!event.locals.user) {
|
|
17
|
-
response.headers.set('Set-Cookie', `session=; Path=/; HttpOnly; SameSite=Lax; Expires=${new Date().toUTCString()}`)
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
14
|
// Invoked for each endpoint called and initially for SSR router
|
|
22
15
|
export const handle: Handle = async ({ event, resolve }) => {
|
|
16
|
+
const { cookies } = event
|
|
17
|
+
const sessionId = cookies.get('session')
|
|
23
18
|
|
|
24
19
|
// before endpoint or page is called
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
await attachUserToRequest(cookies.session, event)
|
|
20
|
+
if (sessionId) {
|
|
21
|
+
await attachUserToRequestEvent(sessionId, event)
|
|
28
22
|
}
|
|
29
23
|
|
|
30
24
|
const response = await resolve(event)
|
|
31
25
|
|
|
32
26
|
// after endpoint or page is called
|
|
33
|
-
|
|
27
|
+
if (!event.locals.user) cookies.delete('session')
|
|
28
|
+
|
|
34
29
|
return response
|
|
35
30
|
}
|
|
@@ -4,8 +4,9 @@ import type { PageServerLoad } from './$types'
|
|
|
4
4
|
export const load: PageServerLoad = async ({locals})=> {
|
|
5
5
|
const { user } = locals
|
|
6
6
|
const authorized = ['admin']
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
console.log('admin/+page.server.ts', user)
|
|
8
|
+
if (!user || !authorized.includes(user.role)) {
|
|
9
|
+
throw redirect(302, '/login?referrer=/admin')
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
return {
|
|
@@ -16,11 +16,12 @@ export const POST: RequestHandler = async (event) => {
|
|
|
16
16
|
sql = `CALL delete_session($1);`
|
|
17
17
|
result = await query(sql, [event.locals.user.id])
|
|
18
18
|
}
|
|
19
|
-
return
|
|
19
|
+
return json({ message: 'Logout successful.' }, {
|
|
20
20
|
headers: {
|
|
21
21
|
'Set-Cookie': `session=; Path=/; SameSite=Lax; HttpOnly; Expires=${new Date().toUTCString()}`
|
|
22
22
|
}
|
|
23
23
|
})
|
|
24
|
+
|
|
24
25
|
case 'login':
|
|
25
26
|
sql = `SELECT authenticate($1) AS "authenticationResult";`
|
|
26
27
|
break
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { error } from '@sveltejs/kit'
|
|
1
|
+
import { error, json } from '@sveltejs/kit'
|
|
2
2
|
import type { RequestHandler } from './$types'
|
|
3
3
|
import { OAuth2Client } from 'google-auth-library'
|
|
4
4
|
import { query } from '../../_db';
|
|
@@ -51,15 +51,14 @@ export const POST: RequestHandler = async event => {
|
|
|
51
51
|
// Prevent hooks.ts's handler() from deleting cookie thinking no one has authenticated
|
|
52
52
|
event.locals.user = userSession.user
|
|
53
53
|
|
|
54
|
-
return
|
|
54
|
+
return json({
|
|
55
55
|
message: 'Successful Google Sign-In.',
|
|
56
56
|
user: userSession.user
|
|
57
|
-
}
|
|
57
|
+
}, {
|
|
58
58
|
headers: {
|
|
59
59
|
'Set-Cookie': `session=${userSession.id}; Path=/; SameSite=Lax; HttpOnly;`}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
})
|
|
61
|
+
|
|
63
62
|
} catch (err) {
|
|
64
63
|
let message = ''
|
|
65
64
|
if (err instanceof Error) message = err.message
|
|
@@ -5,7 +5,7 @@ export const load: PageServerLoad = async ({ locals }) => {
|
|
|
5
5
|
const { user } = locals // populated by /src/hooks.ts
|
|
6
6
|
|
|
7
7
|
const authorized = ['admin', 'teacher', 'student'] // must be logged-in
|
|
8
|
-
if (user
|
|
8
|
+
if (!user || !authorized.includes(user.role)) {
|
|
9
9
|
throw redirect(302, '/login?referrer=/profile')
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -2,8 +2,9 @@ import { redirect } from '@sveltejs/kit'
|
|
|
2
2
|
import type { PageServerLoad } from './$types'
|
|
3
3
|
|
|
4
4
|
export const load: PageServerLoad = async ({locals}) => {
|
|
5
|
+
const { user } = locals
|
|
5
6
|
const authorized = ['admin', 'teacher']
|
|
6
|
-
if (!
|
|
7
|
+
if (!user || !authorized.includes(user.role)) {
|
|
7
8
|
throw redirect(302, '/login?referrer=/teachers')
|
|
8
9
|
}
|
|
9
10
|
|