added login backend
This commit is contained in:
35
app/pages/login.vue
Normal file
35
app/pages/login.vue
Normal file
@@ -0,0 +1,35 @@
|
||||
<script setup lang="ts">
|
||||
const { loggedIn, user, fetch: refreshSession } = useUserSession();
|
||||
|
||||
// create data model for the form
|
||||
const credentials = reactive({
|
||||
email: '',
|
||||
password: '',
|
||||
})
|
||||
|
||||
async function sendLogin() {
|
||||
try {
|
||||
await $fetch("/api/login", {
|
||||
method: "POST",
|
||||
body: credentials
|
||||
})
|
||||
|
||||
await refreshSession()
|
||||
await navigateTo('/dashboard') // user home page
|
||||
|
||||
} catch {
|
||||
alert("Login failed.")
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<UContainer class="overflow-auto">
|
||||
|
||||
|
||||
</UContainer>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
||||
25
server/api/login.post.ts
Normal file
25
server/api/login.post.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
const bodySchema = z.object({
|
||||
email: z.string().email(),
|
||||
password: z.string(),
|
||||
})
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { email, password } = await readValidatedBody(event, bodySchema.parse)
|
||||
|
||||
if (email === 'admin@admin.com' && password === 'iamtheadmin') {
|
||||
// set the user session in the cookie
|
||||
// this server util is auto-imported by the auth-utils module
|
||||
await setUserSession(event, {
|
||||
user: {
|
||||
name: 'John Doe',
|
||||
},
|
||||
})
|
||||
return {}
|
||||
}
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'Bad credentials',
|
||||
})
|
||||
})
|
||||
9
server/api/register.post.ts
Normal file
9
server/api/register.post.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
const bodyPost = z.object({
|
||||
email: z.string().email(),
|
||||
first_name: z.string(),
|
||||
last_name: z.string(),
|
||||
pwd: z.string()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user