Skip to content

Commit

Permalink
feat: add from redirect for auth (#18)
Browse files Browse the repository at this point in the history
* feat: add from redirect for auth

* fix: add missing import
  • Loading branch information
ellisio authored Nov 15, 2022
1 parent fc9a1ab commit 8f13cb8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion components/user-auth-form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client"

import * as React from "react"
import { useSearchParams } from "next/navigation"
import { signIn } from "next-auth/react"
import * as z from "zod"
import { useForm } from "react-hook-form"
Expand All @@ -24,14 +25,15 @@ export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
resolver: zodResolver(userAuthSchema),
})
const [isLoading, setIsLoading] = React.useState<boolean>(false)
const searchParams = useSearchParams();

async function onSubmit(data: FormData) {
setIsLoading(true)

const signInResult = await signIn("email", {
email: data.email.toLowerCase(),
redirect: false,
callbackUrl: `${window.location.origin}/dashboard`,
callbackUrl: searchParams.get("from") || "/dashboard",
})

setIsLoading(false)
Expand Down
9 changes: 8 additions & 1 deletion middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ export default withAuth(
}

if (!isAuth) {
return NextResponse.redirect(new URL("/login", req.url))
let from = req.nextUrl.pathname;
if (req.nextUrl.search) {
from += req.nextUrl.search;
}

return NextResponse.redirect(
new URL(`/login?from=${encodeURIComponent(from)}`, req.url)
);
}
},
{
Expand Down

1 comment on commit 8f13cb8

@vercel
Copy link

@vercel vercel bot commented on 8f13cb8 Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

taxonomy – ./

taxonomyapp.vercel.app
tx.shadcn.com
taxonomy-shadcn.vercel.app
taxonomy-git-main-shadcn.vercel.app

Please sign in to comment.