Skip to content

Commit

Permalink
fix: auth and note by id bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus64 committed Jan 6, 2024
1 parent dadaf24 commit 1238d38
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
14 changes: 13 additions & 1 deletion src/components/Navbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@
import('@sweetalert2/theme-dark/dark.css')
}
onMount(async () => updateUserInfo())
onMount(async () => {
const data = await updateUserInfo()
if (!data) {
return
}
user.set({
email: data.email,
loggedIn: true,
tag: data.tag
})
})
async function logOut() {
await signOut()
Expand Down
3 changes: 1 addition & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ export const versions = [
},
]

export const API_BASE_URL = 'https://bible-api.deno.dev'
// export const API_BASE_URL = 'http://localhost:8000'
export const API_BASE_URL = import.meta.env.VITE_PUBLIC_API_URL ?? 'http://localhost:8000'

export const DEFAULT_NOTE = `
<div>
Expand Down
9 changes: 5 additions & 4 deletions src/routes/notes/[id]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<script>
import { goto } from '$app/navigation'
import Button from '@/components/Button.svelte'
import Editor from '@/components/Editor.svelte'
import Create from '@/components/icons/Create.svelte'
import Lock from '@/components/icons/Lock.svelte'
import { API_BASE_URL } from '@/constants'
import { createAlert } from '@/services/alert.js'
import { updateUserInfo } from '@/services/api/auth.js'
import { editable, getNote, editableNote, sendCreateNote } from '@/state/notes.js'
import { draft } from '@/state/study.js'
import { user } from '@/state/user.js'
import { isValidNote } from '@/utils/note.js'
import { onMount } from 'svelte'
Expand Down Expand Up @@ -117,6 +114,10 @@
}
onMount(async () => {
if (!$user.loggedIn) {
await updateUserInfo()
}
if (!$user.loggedIn) {
goto('/login')
}
Expand Down
73 changes: 52 additions & 21 deletions src/services/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,39 @@ export async function singIn(data) {
createAlert(message, 'error')
}

/**
* @type {usrDat}
*/
const usr = await response.json()

if (usr) {
const userInfo = {
tag: usr.user,
email: usr.email,
loggedIn: true
}

user.set({
email: usr.email,
tag: usr.user,
loggedIn: true
})

localStorage.setItem('user', JSON.stringify(userInfo))
}

return response
}

/**
* @typedef usrDat {
* @property {string} user
* @property {string} email
* @property {boolean} loggedIn
* }
*/
/**
*
*
* @param {string} book
* @returns {Promise<{
Expand Down Expand Up @@ -91,6 +120,24 @@ export async function singUp(data) {
createAlert(message, 'error')
}


/** @type {usrDat} */
const usrData = await response.json()

user.set({
tag: usrData.user,
email: usrData.email,
loggedIn: true
})

const info = {
tag: usrData.user,
email: usrData.email,
loggedIn: true
}

localStorage.setItem('user', JSON.stringify(info))

return response
}

Expand Down Expand Up @@ -124,32 +171,16 @@ export async function updateUserInfo() {
const local = localStorage.getItem('user')
const existLocal = local !== null
if (existLocal) {

/**
* @type {import('@/state/user').User}
*/
const usr = JSON.parse(local)

user.set({
email: usr.email,
loggedIn: true,
tag: usr.tag
})

return user
return usr
}

const res = await fetch(`${API_BASE_URL}/user`, {
credentials: 'include'
})

if (res.ok) {
const info = await res.json()
localStorage.setItem('user', JSON.stringify(info))
user.set({
tag: info.tag,
loggedIn: true,
email: info.email
})

return user
}
} catch (error) {
console.error(error)
return null
Expand Down

0 comments on commit 1238d38

Please sign in to comment.