-
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.
feat: add login form and oauth verification
- Loading branch information
Showing
14 changed files
with
202 additions
and
22 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
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,41 @@ | ||
import type { HttpContext } from '@adonisjs/core/http' | ||
|
||
export default class AuthenticationController { | ||
async login() {} | ||
|
||
async initiateGithubLogin({ ally, inertia }: HttpContext) { | ||
const url = await ally.use('github').redirectUrl() | ||
return inertia.location(url) | ||
} | ||
|
||
async callbackForGithubLogin({ response, ally }: HttpContext) { | ||
const github = ally.use('github') | ||
const user = await github.user() | ||
|
||
return response.json({ user }) | ||
} | ||
|
||
async initiateGoogleLogin({ ally, inertia }: HttpContext) { | ||
const url = await ally.use('google').redirectUrl() | ||
return inertia.location(url) | ||
} | ||
|
||
async callbackForGoogleLogin({ response, ally }: HttpContext) { | ||
const google = ally.use('google') | ||
const user = await google.user() | ||
|
||
return response.json({ user }) | ||
} | ||
|
||
async initiateLinkedinLogin({ ally, inertia }: HttpContext) { | ||
const url = await ally.use('linkedin').redirectUrl() | ||
return inertia.location(url) | ||
} | ||
|
||
async callbackForLinkedinLogin({ response, ally }: HttpContext) { | ||
const linkedin = ally.use('linkedin') | ||
const user = await linkedin.user() | ||
|
||
return response.json({ user }) | ||
} | ||
} |
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,8 @@ | ||
export const messages = { | ||
auth: { | ||
oauth: { | ||
accessDenied: 'O pedido de início de sessão foi rejeitado.', | ||
stateMismatch: 'Ocorreu um erro ao iniciar sessão. Por favor, tenta novamente.', | ||
}, | ||
}, | ||
} as const |
34 changes: 34 additions & 0 deletions
34
website/app/middleware/verify_social_callback_middleware.ts
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,34 @@ | ||
import { SocialProviders } from '@adonisjs/ally/types' | ||
import type { HttpContext } from '@adonisjs/core/http' | ||
import type { NextFn } from '@adonisjs/core/types/http' | ||
import { messages } from '../messages.js' | ||
|
||
export default class VerifySocialCallbackMiddleware { | ||
async handle( | ||
{ response, session, ally }: HttpContext, | ||
next: NextFn, | ||
options: { provider: keyof SocialProviders } | ||
) { | ||
const oauth = ally.use(options.provider) | ||
|
||
if (oauth.accessDenied()) { | ||
session.flashErrors({ oauth: messages.auth.oauth.accessDenied }) | ||
return response.redirect('/login') | ||
} | ||
|
||
if (oauth.stateMisMatch()) { | ||
session.flashErrors({ oauth: messages.auth.oauth.stateMismatch }) | ||
return response.redirect('/login') | ||
} | ||
|
||
const postRedirectError = oauth.getError() | ||
if (postRedirectError) { | ||
session.flashErrors({ oauth: postRedirectError }) | ||
return response.redirect('/login') | ||
} | ||
|
||
const output = await next() | ||
|
||
return output | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { createTuyau } from '@tuyau/client' | ||
import { api } from '#.adonisjs/api' | ||
|
||
console.log(import.meta.env.BASE_URL) | ||
export const tuyau = createTuyau({ | ||
api, | ||
baseUrl: import.meta.env.BASE_URL || 'http://localhost:3333', | ||
baseUrl: 'http://localhost:3333', | ||
}) |
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,29 @@ | ||
import { usePage } from '@inertiajs/react' | ||
import { useEffect, useMemo } from 'react' | ||
import { Toast, toast } from './use_toast' | ||
|
||
export function useError(key: string) { | ||
const page = usePage() | ||
const error = useMemo(() => { | ||
const props = page.props | ||
if (props.errors && key in props.errors) { | ||
return props.errors[key] | ||
} | ||
|
||
return null | ||
}, [page.props]) | ||
|
||
return error | ||
} | ||
|
||
export function useErrorToast(key: string, toastCreator: (msg: string) => Toast) { | ||
const error = useError(key) | ||
const toastContent = useMemo(() => error && toastCreator(error), [error]) | ||
|
||
useEffect(() => { | ||
if (toastContent) { | ||
const t = toast(toastContent) | ||
return () => t.dismiss() | ||
} | ||
}, [toastContent]) | ||
} |
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