feat(WEB-132): add middlewares, support for seamless login and improve lang based routes
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import { redirect } from "next/navigation"
|
||||
|
||||
import { auth } from "@/auth"
|
||||
import { auth, signIn } from "@/auth"
|
||||
|
||||
import type { LangParams, LayoutArgs } from "@/types/params"
|
||||
|
||||
@@ -14,7 +12,10 @@ export default async function ProtectedLayout({
|
||||
* protected route group is actually protected.
|
||||
*/
|
||||
if (!session) {
|
||||
return redirect(`/${params.lang}/login`)
|
||||
await signIn("curity", undefined, {
|
||||
ui_locales: params.lang,
|
||||
})
|
||||
return null
|
||||
}
|
||||
|
||||
return <>{children}</>
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import { signIn } from "@/auth"
|
||||
import { pageNames } from "@/constants/myPages"
|
||||
|
||||
import type { LangParams, Params } from "@/types/params"
|
||||
|
||||
export default async function Page({ params }: Params<LangParams>) {
|
||||
async function login() {
|
||||
"use server"
|
||||
await signIn("curity", {
|
||||
redirectTo: `/${params.lang}/${pageNames[params.lang]}`,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<main>
|
||||
<form action={login}>
|
||||
<button type="submit">Sign In</button>
|
||||
</form>
|
||||
</main>
|
||||
)
|
||||
}
|
||||
47
app/[lang]/(live)/(public)/login/route.ts
Normal file
47
app/[lang]/(live)/(public)/login/route.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { AuthError } from "next-auth"
|
||||
|
||||
import { signIn } from "@/auth"
|
||||
import { badRequest } from "@/server/errors/next"
|
||||
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
context: { params: { lang: Lang } }
|
||||
) {
|
||||
const redirectTo =
|
||||
request.headers.get("x-redirect-to") ||
|
||||
request.nextUrl.searchParams.get("redirectTo") ||
|
||||
undefined
|
||||
|
||||
try {
|
||||
/**
|
||||
* Passing `redirect: false` to `signIn` will return the URL instead of
|
||||
* automatically redirecting to it inside of `signIn`.
|
||||
* https://github.com/nextauthjs/next-auth/blob/3c035ec/packages/next-auth/src/lib/actions.ts#L76
|
||||
*/
|
||||
const url = await signIn(
|
||||
"curity",
|
||||
{
|
||||
redirectTo,
|
||||
redirect: false,
|
||||
},
|
||||
{
|
||||
ui_locales: context.params.lang,
|
||||
}
|
||||
)
|
||||
|
||||
if (url) {
|
||||
return NextResponse.redirect(url)
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
console.log({ signInAuthError: error })
|
||||
} else {
|
||||
console.log({ signInError: error })
|
||||
}
|
||||
}
|
||||
|
||||
return badRequest()
|
||||
}
|
||||
Reference in New Issue
Block a user