-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sign up function to return data, test written
- Loading branch information
1 parent
7a4ed34
commit e3bf0fc
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<script setup> | ||
import { ref } from "vue"; | ||
import { definePageMeta, useAuth } from "#imports"; | ||
const { signUp } = useAuth(); | ||
const username = ref(""); | ||
const password = ref(""); | ||
const response = ref() | ||
const register = async () => { | ||
try { | ||
const signUpResponse = await signUp({username: username.value, password: password.value}, undefined, { preventLoginFlow: true }) | ||
response.value = signUpResponse | ||
} catch (error) { | ||
response.value = { error: "Failed to sign up" }; | ||
} | ||
} | ||
definePageMeta({ | ||
auth: { | ||
unauthenticatedOnly: true, | ||
navigateAuthenticatedTo: "/", | ||
}, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<form @submit.prevent="register"> | ||
<p><i>*password should have at least 6 characters</i></p> | ||
<input v-model="username" type="text" placeholder="Username" data-testid="regUsername"> | ||
<input v-model="password" type="password" placeholder="Password" data-testid="regPassword"> | ||
<button type="submit" data-testid="regSubmit"> | ||
sign up | ||
</button> | ||
</form> | ||
<div v-if="response"> | ||
<h2>Response</h2> | ||
<pre data-testid="regResponse">{{ response }}</pre> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { createError, eventHandler, readBody } from 'h3' | ||
import { z } from 'zod' | ||
import { sign } from 'jsonwebtoken' | ||
|
||
export const SECRET = 'dummy' | ||
|
||
export default eventHandler(async (event) => { | ||
// Define the schema for validating the incoming data | ||
const result = z.object({ | ||
username: z.string(), | ||
password: z.string().min(6) | ||
}).safeParse(await readBody(event)) | ||
|
||
// If validation fails, return an error | ||
if (!result.success) { | ||
throw createError({ | ||
statusCode: 400, | ||
statusMessage: 'Invalid input, please provide a valid email and a password of at least 6 characters.' | ||
}) | ||
} | ||
|
||
const { username, password } = result.data | ||
|
||
const expiresIn = '1h' //token expiry (1 hour) | ||
const user = { username } // Payload for the token, includes the email | ||
|
||
// Sign the JWT with the user payload and secret | ||
const accessToken = sign(user, SECRET, { expiresIn }) | ||
|
||
// Return a success response with the email and the token | ||
return { | ||
message: 'Signup successful!', | ||
user: { | ||
username | ||
}, | ||
token: { | ||
accessToken | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters