Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: redirect to original path after sign in - [SUPA-49] #95

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading