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

Feature/fix oauth login #243

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
134 changes: 85 additions & 49 deletions app/api/auth/signup/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,103 @@
import { createClient } from '@/utils/supabase/client';
import { createClient as createServerClient } from '@/utils/supabase/server';
import { createServerClient, type CookieOptions } from '@supabase/ssr';
import { cookies } from 'next/headers';
import { NextResponse } from 'next/server';

export async function GET(request: Request) {
try {
const supabaseServerClient = createServerClient()
const supabaseClient = createClient()
const requestUrl = new URL(request.url);
const { searchParams } = requestUrl;
const code = searchParams.get('code');

if (!code) {
return NextResponse.redirect('/auth/auth-code-error');
const next = searchParams.get('next') ?? '/';
if (code) {
const cookieStore = cookies();
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get(name: string) {
return cookieStore.get(name)?.value;
},
set(name: string, value: string, options: CookieOptions) {
cookieStore.set({ name, value, ...options });
},
remove(name: string, options: CookieOptions) {
cookieStore.delete({ name, ...options });
},
},
},
);
const { error } = await supabase.auth.exchangeCodeForSession(code);
if (!error) {
return NextResponse.redirect(requestUrl.origin + '/profile');
}
}
// return the user to an error page with instructions
return NextResponse.redirect('/auth/auth-code-error');
}

const { error, data } = await supabaseServerClient.auth.exchangeCodeForSession(code);
// import { createClient } from '@/utils/supabase/client';
// import { createClient as createServerClient } from '@/utils/supabase/server';
// import { NextResponse } from 'next/server';

// export async function GET(request: Request) {
// try {
// const supabaseServerClient = createServerClient()
// const supabaseClient = createClient()
// const requestUrl = new URL(request.url);
// const { searchParams } = requestUrl;
// const code = searchParams.get('code');

if (error) {
console.error('Error exchanging code for session:', error);
return NextResponse.redirect('/auth/auth-code-error');
}
// if (!code) {
// return NextResponse.redirect('/auth/auth-code-error');
// }

const metadata = data.user.user_metadata;
// const { error, data } = await supabaseServerClient.auth.exchangeCodeForSession(code);

if (!metadata.email) {
return NextResponse.redirect('/auth/auth-code-error');
}
// if (error) {
// console.error('Error exchanging code for session:', error);
// return NextResponse.redirect('/auth/auth-code-error');
// }

const query = await supabaseClient
.from('User')
.select('*')
.eq('email', metadata.email);
// const metadata = data.user.user_metadata;

if (query.data?.length === 0) {
const { data, error } = await supabaseServerClient.auth.signUp({
email: metadata.email,
password: metadata.password,
options: {
data: {
email: metadata.email,
},
},
});
// if (!metadata.email) {
// return NextResponse.redirect('/auth/auth-code-error');
// }

if (error) {
console.error('Error signing up user with provider:', error);
return NextResponse.json(
{ error: 'An error occurred during sign up.' },
{ status: 400 }
);
}
// const query = await supabaseClient
// .from('User')
// .select('*')
// .eq('email', metadata.email);

return NextResponse.json({ success: true, data });
}
// if (query.data?.length === 0) {
// const { data, error } = await supabaseServerClient.auth.signUp({
// email: metadata.email,
// password: metadata.password,
// options: {
// data: {
// email: metadata.email,
// },
// },
// });

return NextResponse.redirect(requestUrl.origin + '/profile');
// if (error) {
// console.error('Error signing up user with provider:', error);
// return NextResponse.json(
// { error: 'An error occurred during sign up.' },
// { status: 400 }
// );
// }

} catch (error) {
console.error('Error handling signup request:', error);
return NextResponse.json(
{ error: 'An internal server error occurred.' },
{ status: 500 }
);
}
}
// return NextResponse.json({ success: true, data });
// }

// return NextResponse.redirect(requestUrl.origin + '/profile');

// } catch (error) {
// console.error('Error handling signup request:', error);
// return NextResponse.json(
// { error: 'An internal server error occurred.' },
// { status: 500 }
// );
// }
// }
25 changes: 11 additions & 14 deletions app/api/user/getUserId/route.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@

import { createClient } from '@/utils/supabase/server';
import { NextResponse } from 'next/server';

export async function GET(request: Request) {
const supabase = createClient();

try {

const {
data: { user },
} = await supabase.auth.getUser();
const userId = user?.id
const supabase = await createClient();

return NextResponse.json({ success: true, userId });
try {
const {
data: { user },
} = await supabase.auth.getUser();
const userId = user?.id;

} catch (err) {
return NextResponse.json({ error: 'Failed to fetch user ID' });
}
}
return NextResponse.json({ success: true, userId });
} catch (err) {
return NextResponse.json({ error: 'Failed to fetch user ID' });
}
}
118 changes: 58 additions & 60 deletions components/authentication/hooks/useSocialAuth.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,66 @@
import { createClient } from '@/utils/supabase/client';

const useSocialAuth = () => {
const supabase = createClient()
const supabase = createClient();


const signUpWithGoogle = () => {
supabase.auth.signInWithOAuth({
provider: 'google',
options: {
queryParams: {
access_type: 'offline',
prompt: 'consent',
},
redirectTo: `${location.origin}/api/auth/signup/callback/`,
},
});
};

const loginWithGoogle = () => {
supabase.auth.signInWithOAuth({
provider: 'google',
options: {
queryParams: {
access_type: 'offline',
prompt: 'consent',
},
redirectTo: `${location.origin}/api/auth/callback/`,
},
});
};

const loginWithFacebook = () => {
supabase.auth.signInWithOAuth({
provider: 'facebook',
options: {
queryParams: {
access_type: 'offline',
prompt: 'consent',
},
redirectTo: `${location.origin}/api/auth/callback/`,
},
});
};

const signUpWithGoogle = () => {
supabase.auth.signInWithOAuth({
provider: 'google',
options: {
queryParams: {
access_type: 'offline',
prompt: 'consent',
},
redirectTo: `${location.origin}/api/auth/signup/callback/`,
const loginWithApple = () => {
supabase.auth.signInWithOAuth({
provider: 'apple',
options: {
queryParams: {
access_type: 'offline',
prompt: 'consent',
},
});
};

const loginWithGoogle = () => {
supabase.auth.signInWithOAuth({
provider: 'google',
options: {
queryParams: {
access_type: 'offline',
prompt: 'consent',
},
redirectTo: `${location.origin}/api/auth/callback/`,
},
});
};

const loginWithFacebook = () => {
supabase.auth.signInWithOAuth({
provider: 'facebook',
options: {
queryParams: {
access_type: 'offline',
prompt: 'consent',
},
redirectTo: `${location.origin}/api/auth/callback/`,
},
});
};

const loginWithApple = () => {
supabase.auth.signInWithOAuth({
provider: 'apple',
options: {
queryParams: {
access_type: 'offline',
prompt: 'consent',
},
redirectTo: `${location.origin}/api/auth/callback/`,
},
});
};
redirectTo: `${location.origin}/api/auth/callback/`,
},
});
};

return {
signUpWithGoogle,
loginWithGoogle,
loginWithFacebook,
loginWithApple
}
}
return {
signUpWithGoogle,
loginWithGoogle,
loginWithFacebook,
loginWithApple,
};
};

export default useSocialAuth;
export default useSocialAuth;
Loading