Skip to content

Commit

Permalink
feat: redirect to original path after sign in
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Dec 16, 2023
1 parent 221edb4 commit 9645ddb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion app/(auth)/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ZodError } from 'zod';
export async function GET(request: NextRequest) {
const requestUrl = new URL(request.url);
const code = requestUrl.searchParams.get('code');
const redirect = requestUrl.searchParams.get('redirect');

if (!code) {
return new HttpBadRequestError({
Expand All @@ -35,6 +36,7 @@ export async function GET(request: NextRequest) {

try {
const { isNewAccount } = await saveUserInfo({ session, user });

if (isNewAccount) {
return NextResponse.redirect(
new URL('/onboarding/start', request.url).toString(),
Expand All @@ -43,7 +45,8 @@ export async function GET(request: NextRequest) {
},
);
}
return NextResponse.redirect(requestUrl.origin);

return NextResponse.redirect(new URL(redirect || '/shows', request.url));
} catch (error) {
switch (true) {
case error instanceof DatabaseError:
Expand Down
5 changes: 5 additions & 0 deletions app/(auth)/auth/sign-in/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { NextResponse } from 'next/server';

export async function POST(request: NextRequest) {
const supabase = createSupabaseServerClient(cookies());

const formData = await request.formData();
const redirect = String(formData.get('redirect'));

const redirectURL = new URL('/auth/callback', request.url);
redirectURL.searchParams.set('redirect', redirect);

const scopes =
'user-read-email user-read-playback-position user-library-read';
Expand Down
14 changes: 13 additions & 1 deletion app/(auth)/sign-in/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Button, Flex, Heading, Link, Text } from '@radix-ui/themes';
import { FaSpotify } from 'react-icons/fa';

export default function Page() {
type Props = {
searchParams: {
redirect?: string;
};
};

export default function Page(props: Props) {
return (
<Flex direction="column" gap="4">
<Heading trim="both">Welcome to beecast</Heading>
Expand All @@ -11,6 +17,12 @@ export default function Page() {
</Text>

<form action="/auth/sign-in" method="POST">
<input
name="redirect"
type="hidden"
value={props.searchParams.redirect}
/>

<Flex direction="column">
<Button highContrast size="3" type="submit">
<FaSpotify />
Expand Down
6 changes: 5 additions & 1 deletion middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export async function middleware(request: NextRequest) {
return response;
}

return NextResponse.redirect(new URL('/sign-in', request.url));
const nextURL = new URL(request.url);
nextURL.pathname = '/sign-in';
nextURL.searchParams.set('redirect', pathname);

return NextResponse.redirect(nextURL);
}

export const config = {
Expand Down

0 comments on commit 9645ddb

Please sign in to comment.