-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from aceleradora-TW/106/refactor-login-para-s…
…erver-side 106/refactor login para server side
- Loading branch information
Showing
8 changed files
with
87 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
import NextAuth from "next-auth" | ||
import { getAuthProviders } from "../../../helpers/providers"; | ||
import { getAuthProviders } from "../../../helpers/providers" | ||
|
||
const handler = NextAuth({ | ||
providers: getAuthProviders(), | ||
|
||
callbacks: { | ||
async jwt({ token, user, account }) { | ||
if (user && account) { | ||
token.id = user.id; | ||
token.provider = account.provider; | ||
token.accessToken = account.access_token; | ||
token.id = user.id | ||
token.provider = account.provider | ||
token.accessToken = account.access_token | ||
} | ||
return token; | ||
return token | ||
}, | ||
}, | ||
}); | ||
|
||
}) | ||
|
||
export { handler as GET, handler as POST } |
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,43 +1,13 @@ | ||
"use client" | ||
|
||
import { CardLogin } from "@/components/CardLogin" | ||
import { LayoutPage } from "@/components/PageElements/LayoutPage" | ||
import { useSession } from "next-auth/react" | ||
import { useRouter, useSearchParams } from "next/navigation" | ||
import { Suspense, useEffect } from "react" | ||
import { Loading } from "@/components/Loading" | ||
|
||
function LoginContent() { | ||
const { data: session, status } = useSession() | ||
const router = useRouter() | ||
const searchParams = useSearchParams() | ||
const callbackUrl = searchParams?.get("callbackUrl") || "/" | ||
|
||
useEffect(() => { | ||
if (status === "authenticated") { | ||
router.push(callbackUrl) | ||
} | ||
}, [status, router, callbackUrl]) | ||
|
||
|
||
if (status === "loading") { | ||
return <Loading /> | ||
} | ||
export default function LoginPage() { | ||
|
||
if (status === "unauthenticated") { | ||
return ( | ||
<LayoutPage> | ||
<CardLogin /> | ||
</LayoutPage> | ||
) | ||
} | ||
|
||
return null | ||
return ( | ||
<LayoutPage> | ||
<CardLogin /> | ||
</LayoutPage> | ||
) | ||
} | ||
|
||
export default function Login() { | ||
|
||
return (<Suspense fallback={<Loading />}> | ||
<LoginContent /> | ||
</Suspense>) | ||
} |
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,23 @@ | ||
import { NextResponse } from "next/server" | ||
import type { NextRequest } from "next/server" | ||
|
||
export async function middleware(request: NextRequest) { | ||
const sessionToken = | ||
request.cookies.get("next-auth.session-token") || | ||
request.cookies.get("__Secure-next-auth.session-token") | ||
|
||
const callbackUrl = | ||
new URLSearchParams(request.nextUrl.search).get("callbackUrl") || "/" | ||
|
||
const parsedUrl = new URL(callbackUrl, request.url) | ||
|
||
if (request.nextUrl.pathname.startsWith("/login") && sessionToken) { | ||
return NextResponse.redirect(parsedUrl) | ||
} | ||
|
||
return NextResponse.next() | ||
} | ||
|
||
export const config = { | ||
matcher: ["/login"], | ||
} |
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,13 @@ | ||
import NextAuth, { DefaultSession } from "next-auth"; | ||
|
||
declare module "next-auth" { | ||
/** | ||
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context | ||
*/ | ||
interface Session { | ||
user: { | ||
/** Oauth access token */ | ||
token?: accessToken; | ||
} & DefaultSession["user"]; | ||
} | ||
} |