sveltekit-auth-example 1.0.13 → 1.0.16

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.
Files changed (41) hide show
  1. package/.eslintrc.cjs +1 -1
  2. package/.prettierignore +13 -0
  3. package/.prettierrc +4 -1
  4. package/CHANGELOG.md +14 -1
  5. package/README.md +16 -19
  6. package/package.json +19 -18
  7. package/src/app.d.ts +5 -5
  8. package/src/app.html +1 -0
  9. package/src/hooks.ts +3 -13
  10. package/src/lib/auth.ts +49 -75
  11. package/src/routes/+error.svelte +6 -0
  12. package/src/routes/+layout.server.ts +9 -0
  13. package/src/routes/{__layout.svelte → +layout.svelte} +18 -14
  14. package/src/routes/{index.svelte → +page.svelte} +0 -0
  15. package/src/routes/admin/+page.server.ts +14 -0
  16. package/src/routes/admin/+page.svelte +12 -0
  17. package/src/routes/api/v1/user/+server.ts +21 -0
  18. package/src/routes/auth/[slug]/+server.ts +66 -0
  19. package/src/routes/auth/{forgot.ts → forgot/+server.ts} +4 -6
  20. package/src/routes/auth/{google.ts → google/+server.ts} +16 -20
  21. package/src/routes/auth/reset/{index.ts → +server.ts} +10 -13
  22. package/src/routes/auth/reset/{[token].svelte → [token]/+page.svelte} +4 -15
  23. package/src/routes/auth/reset/[token]/+page.ts +7 -0
  24. package/src/routes/{forgot.svelte → forgot/+page.svelte} +2 -2
  25. package/src/routes/{info.svelte → info/+page.svelte} +0 -0
  26. package/src/routes/{login.svelte → login/+page.svelte} +3 -3
  27. package/src/routes/profile/+page.server.ts +15 -0
  28. package/src/routes/{profile.svelte → profile/+page.svelte} +7 -25
  29. package/src/routes/register/+page.server.ts +10 -0
  30. package/src/routes/{register.svelte → register/+page.svelte} +6 -23
  31. package/src/routes/teachers/+page.server.ts +13 -0
  32. package/src/routes/teachers/+page.svelte +12 -0
  33. package/src/stores.ts +11 -1
  34. package/src/routes/__error.svelte +0 -19
  35. package/src/routes/admin.svelte +0 -40
  36. package/src/routes/api/v1/_auth.ts +0 -5
  37. package/src/routes/api/v1/admin.ts +0 -20
  38. package/src/routes/api/v1/teacher.ts +0 -20
  39. package/src/routes/api/v1/user.ts +0 -35
  40. package/src/routes/auth/[slug].ts +0 -93
  41. package/src/routes/teachers.svelte +0 -39
@@ -1,93 +0,0 @@
1
- import type { RequestHandler } from '@sveltejs/kit'
2
- import { query } from '../_db'
3
-
4
- export const POST: RequestHandler = async event => {
5
- const { slug } = event.params
6
-
7
- let result
8
- let sql
9
-
10
- try {
11
- switch (slug) {
12
- case 'logout':
13
- if (event.locals.user) { // if user is null, they are logged out anyway (session might have ended)
14
- sql = `CALL delete_session($1);`
15
- result = await query(sql, [event.locals.user.id])
16
- }
17
- return {
18
- status: 200,
19
- headers: {
20
- 'Set-Cookie': `session=; Path=/; SameSite=Lax; HttpOnly; Expires=${new Date().toUTCString()}`
21
- },
22
- body: {
23
- message: 'Logout successful.'
24
- }
25
- }
26
- case 'login':
27
- sql = `SELECT authenticate($1) AS "authenticationResult";`
28
- break
29
- case 'register':
30
- sql = `SELECT register($1) AS "authenticationResult";`
31
- break
32
-
33
- default:
34
- return {
35
- status: 404,
36
- body: {
37
- message: 'Invalid endpoint.',
38
- user: null
39
- }
40
- }
41
- }
42
-
43
- // Only /auth/login and /auth/register at this point
44
- const body = await event.request.json()
45
-
46
- // While client checks for these to be non-null, register() in the database does not
47
- if (slug == 'register' && (!body.email || !body.password || !body.firstName || !body.lastName))
48
- return {
49
- status: 400,
50
- body: {
51
- message: 'Please supply all required fields: email, password, first and last name.',
52
- user: null
53
- }
54
- }
55
-
56
- result = await query(sql, [JSON.stringify(body)])
57
- } catch (error) {
58
- return {
59
- status: 503,
60
- body: {
61
- message: 'Could not communicate with database.',
62
- user: null
63
- }
64
- }
65
- }
66
-
67
- const { authenticationResult }: { authenticationResult: AuthenticationResult } = result.rows[0]
68
-
69
- if (!authenticationResult.user) {
70
- return {
71
- status: authenticationResult.statusCode,
72
- body: {
73
- message: authenticationResult.status,
74
- user: null,
75
- sessionId: null
76
- }
77
- }
78
- }
79
-
80
- // Prevent hooks.ts:handle() from deleting cookie we just set
81
- event.locals.user = authenticationResult.user
82
-
83
- return {
84
- status: authenticationResult.statusCode,
85
- headers: { // database expires sessions in 2 hours (could do it here too)
86
- 'Set-Cookie': `session=${authenticationResult.sessionId}; Path=/; SameSite=Lax; HttpOnly;`
87
- },
88
- body: {
89
- message: authenticationResult.status,
90
- user: authenticationResult.user,
91
- }
92
- }
93
- }
@@ -1,39 +0,0 @@
1
- <script context="module" lang="ts">
2
- import type { Load } from '@sveltejs/kit'
3
-
4
- export const load: Load = async ({ fetch, session }) => {
5
- const authorized = ['admin', 'teacher']
6
- if (!session.user || !authorized.includes(session.user.role)) {
7
- return {
8
- status: 302,
9
- redirect: '/login?referrer=/teachers'
10
- }
11
- }
12
-
13
- const url = '/api/v1/teacher'
14
- const res = await fetch(url, {
15
- method: 'GET',
16
- headers: { 'Content-Type': 'application/json' }
17
- })
18
-
19
- // if !res.ok, error is returned as message
20
- const { message } = await res.json()
21
- return {
22
- props: {
23
- message
24
- }
25
- }
26
- }
27
- </script>
28
-
29
- <script lang="ts">
30
- export let message = ''
31
- </script>
32
-
33
- <svelte:head>
34
- <title>Teachers</title>
35
- </svelte:head>
36
-
37
- <h1>Teachers</h1>
38
- <h4>Teacher Or Admin Role</h4>
39
- <p>{message}</p>