Skip to content

Commit

Permalink
feat: better assert of type in login
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus64 committed Sep 17, 2023
1 parent 8503e89 commit 798847a
Showing 1 changed file with 54 additions and 51 deletions.
105 changes: 54 additions & 51 deletions src/routes/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,60 @@
* @param {Event & { readonly submitter: HTMLElement | null }} event
*/
async function onSubmit(event) {
const formData = Object.fromEntries(new FormData(undefined, event.submitter))
const response = await fetch('https://bible-api.deno.dev/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify(formData)
})
if (!response.ok) {
const error = await response.json()
if (error.message) {
if (error.message.includes('User')) {
createAlert('El usuario no existe', 'error')
return
}
createAlert(error.message, 'error')
}
const message = error.issues
? `Error en el campo ${error.issues[0].path[0]}`
: 'Error al autenticarse'
createAlert(message, 'error')
return
}
await fetch('https://bible-api.deno.dev/user', {
method: 'GET',
credentials: 'include'
})
.then(async (res) => {
if (res.ok) {
const info = await res.json()
user.set({
email: info.email,
tag: info.tag,
loggedIn: true
})
}
})
.catch((err) => {
createAlert(err, 'error')
return
})
goto('/')
if (event.target instanceof HTMLFormElement) {
const formData = Object.fromEntries(new FormData(event.target))
const response = await fetch('https://bible-api.deno.dev/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify(formData)
})
if (!response.ok) {
const error = await response.json()
if (error.message) {
if (error.message.includes('User')) {
createAlert('El usuario no existe', 'error')
return
}
createAlert(error.message, 'error')
}
const message = error.issues
? `Error en el campo ${error.issues[0].path[0]}`
: 'Error al autenticarse'
createAlert(message, 'error')
return
}
await fetch('https://bible-api.deno.dev/user', {
method: 'GET',
credentials: 'include'
})
.then(async (res) => {
if (res.ok) {
const info = await res.json()
user.set({
email: info.email,
tag: info.tag,
loggedIn: true
})
}
})
.catch((err) => {
createAlert(err, 'error')
return
})
goto('/')
}
}
</script>

Expand Down

1 comment on commit 798847a

@vercel
Copy link

@vercel vercel bot commented on 798847a Sep 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

bible-app-ubqz – ./

bible-app-ubqz-git-main-atticus64.vercel.app
bible-app-ubqz-atticus64.vercel.app
bible-study.vercel.app

Please sign in to comment.