11'use server' ;
22
3- import { Routes } from '@/utils/constants' ;
4- import { createClient } from '@/utils/supabase/server' ;
5- import { SEED_USER } from '@onlook/db' ;
6- import { SignInMethod } from '@onlook/models' ;
3+ import {
4+ getRedirectResult ,
5+ GoogleAuthProvider ,
6+ signInWithEmailAndPassword ,
7+ signInWithRedirect ,
8+ } from 'firebase/auth' ;
79import { headers } from 'next/headers' ;
810import { redirect } from 'next/navigation' ;
911
10- export async function login ( provider : SignInMethod ) {
11- const supabase = await createClient ( ) ;
12- const origin = ( await headers ( ) ) . get ( 'origin' ) ;
13-
14- // If already session, redirect
15- const {
16- data : { session } ,
17- } = await supabase . auth . getSession ( ) ;
18- if ( session ) {
19- redirect ( '/' ) ;
20- }
21-
22- // Start OAuth flow
23- // Note: User object will be created in the auth callback route if it doesn't exist
24- const { data, error } = await supabase . auth . signInWithOAuth ( {
25- provider,
26- options : {
27- redirectTo : `${ origin } /auth/callback` ,
28- } ,
29- } ) ;
12+ import { auth } from '@onlook/db/firebase' ;
13+ import { SEED_USER } from '@onlook/db' ;
14+ import { SignInMethod } from '@onlook/models' ;
15+ import { Routes } from '@/utils/constants' ;
3016
31- if ( error ) {
32- redirect ( '/error' ) ;
33- }
17+ export async function login ( provider : SignInMethod ) {
18+ const origin = ( await headers ( ) ) . get ( 'origin' ) ;
19+ const googleProvider = new GoogleAuthProvider ( ) ;
20+
21+ // We can add more providers here based on the `provider` argument
22+ switch ( provider ) {
23+ case 'google' :
24+ await signInWithRedirect ( auth , googleProvider ) ;
25+ break ;
26+ default :
27+ throw new Error ( `Provider ${ provider } not supported` ) ;
28+ }
29+ }
3430
35- redirect ( data . url ) ;
31+ export async function handleRedirect ( ) {
32+ const result = await getRedirectResult ( auth ) ;
33+ if ( result ) {
34+ // User is signed in.
35+ // You can get the user info from result.user
36+ redirect ( Routes . HOME ) ;
37+ } else {
38+ // Handle the case where there is no redirect result
39+ redirect ( '/login' ) ;
40+ }
3641}
3742
3843export async function devLogin ( ) {
39- if ( process . env . NODE_ENV !== 'development' ) {
40- throw new Error ( 'Dev login is only available in development mode' ) ;
41- }
42-
43- const supabase = await createClient ( ) ;
44-
45- const {
46- data : { session } ,
47- } = await supabase . auth . getSession ( ) ;
48- if ( session ) {
49- redirect ( '/' ) ;
50- }
51-
52- const { data, error } = await supabase . auth . signInWithPassword ( {
53- email : SEED_USER . EMAIL ,
54- password : SEED_USER . PASSWORD ,
55- } ) ;
56-
57- if ( error ) {
58- console . error ( 'Error signing in with password:' , error ) ;
59- throw new Error ( 'Error signing in with password' ) ;
60- }
44+ if ( process . env . NODE_ENV !== 'development' ) {
45+ throw new Error ( 'Dev login is only available in development mode' ) ;
46+ }
6147
48+ try {
49+ await signInWithEmailAndPassword ( auth , SEED_USER . EMAIL , SEED_USER . PASSWORD ) ;
6250 redirect ( Routes . HOME ) ;
51+ } catch ( error ) {
52+ console . error ( 'Error signing in with password:' , error ) ;
53+ throw new Error ( 'Error signing in with password' ) ;
54+ }
6355}
0 commit comments