-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
133 additions
and
35 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,62 @@ | ||
import process from 'node:process' | ||
import { type H3Event, eventHandler, readBody } from 'h3' | ||
|
||
export default eventHandler(async (event: H3Event) => { | ||
const body = await readBody(event) | ||
|
||
const tokenResponse = await fetch(`${process.env.NUXT_PUBLIC_BACKEND_URL}/session/token`) | ||
|
||
if (!tokenResponse || tokenResponse.status !== 200) | ||
return new Response('Unauthorized', { status: 401 }) | ||
|
||
const token = await tokenResponse.text() | ||
|
||
if (!token) | ||
return new Response('Unauthorized', { status: 401 }) | ||
|
||
try { | ||
const response = await fetch( | ||
`${process.env.NUXT_PUBLIC_BACKEND_URL}/jwt/token`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-CSRF-Token': token as string, | ||
'Authorization': `Bearer ${body.token}`, | ||
}, | ||
body: JSON.stringify({ | ||
name: body.email, | ||
pass: body.password, | ||
preferred_langcode: [{ value: body.langcode }], | ||
}), | ||
}, | ||
) | ||
|
||
if (!response || response.status !== 200) { | ||
const json = await response.json() | ||
|
||
if (json.message) { | ||
return new Response(JSON.stringify({ | ||
message: json.message, | ||
}), { status: 422 }) | ||
} | ||
|
||
return new Response('Unauthorized', { status: 401 }) | ||
} | ||
|
||
const jwtToken = await response.json() | ||
|
||
if (!jwtToken.token) | ||
return new Response('Unauthorized', { status: 401 }) | ||
|
||
setCookie(event, 'auth.token', jwtToken.token) | ||
|
||
return { | ||
token: jwtToken.token, | ||
} | ||
} | ||
catch (error) { | ||
console.error(error) | ||
return new Response('Unauthorized', { status: 401 }) | ||
} | ||
}) |
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