Skip to content

Commit

Permalink
manading form_create from falling down and allowing user to create ta…
Browse files Browse the repository at this point in the history
…ble if their no entries for them in users table
  • Loading branch information
Anas-github-acc committed Jul 24, 2024
1 parent 58621aa commit 4926a31
Show file tree
Hide file tree
Showing 18 changed files with 596 additions and 106 deletions.
107 changes: 107 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"@radix-ui/react-slot": "^1.1.0",
"@reduxjs/toolkit": "^2.2.6",
"@supabase/ssr": "^0.4.0",
"axios": "^1.7.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"ldrs": "^1.0.2",
"lucide-react": "^0.408.0",
"next": "14.2.5",
"next-client-cookies": "^1.1.1",
Expand Down
10 changes: 10 additions & 0 deletions src/app/api/logout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {NextRequest, NextResponse} from "next/server";
import {createClient} from "@/utils/supabase/server";

export async function GET(req: NextRequest) {
const supabase = createClient()

const { error } = await supabase.auth.signOut()

return NextResponse.redirect(new URL("/form_create", req.url))
}
29 changes: 29 additions & 0 deletions src/app/api/rest/v1/isUsername/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {NextRequest, NextResponse} from "next/server";

export async function POST(req: NextRequest) {
let response = NextResponse.next({
request: {
headers: req.headers,
},
})

const body = await req.json()

if ( !body.username ) {
return NextResponse.json({ error: 'username is required' })
}

let res = await fetch(`${process.env.NEXT_PUBLIC_SUPABASE_URL}/rest/v1/rpc/is_username_exist`, {
method: 'POST',
headers: {
"apikey": process.env.SERVICE_KEY!,
"Authorization": `Bearer ${process.env.SERVICE_KEY!}`,
"Content-Type": "application/json",
},
body: JSON.stringify(body)
});

let data = await res.json();

return NextResponse.json({ state: data });
}
39 changes: 39 additions & 0 deletions src/app/api/rest/v1/users/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {NextRequest, NextResponse} from "next/server";
import { createClient } from "@/utils/supabase/server";
import { User } from "@supabase/supabase-js";

export async function POST(req: NextRequest) {
let response = NextResponse.next({
request: {
headers: req.headers,
},
})

const body = await req.json()
const supabase = createClient();
const searchParams = req.nextUrl.searchParams;
const option = searchParams.get('option');

let data: User | User[] | null = null;

if ( option === 'insert' ) {
const { data: user, error: error } = await supabase
.from('users')
.insert([{id: body.id, username: body.username, admin: body.admin}])
if (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
data = user;
} else if ( option === 'update' ) {
const { data: user, error: error } = await supabase
.from('users')
.update({username: body.username})
.match({id: body.id})
if (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
data = user;
}

return NextResponse.json({ data: data });
}
56 changes: 29 additions & 27 deletions src/app/auth/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,53 @@ export const Login = async (

export const SignUp = async (
credentials : {
username: string | null,
email: string ,
password: string ,
}) => {
const origin = headers().get("origin");
const referer = headers().get("referer");
const query = referer?.split('?')[1].split('&');
const org = query?.find((q) => q.includes('organisation')) || '';

const username = credentials.username;
const supabase = createClient();
const { data: { session }, error, } = await supabase.auth.signUp({

const { data: { user, session }, error, } = await supabase.auth.signUp({
email: credentials.email as string,
password: credentials.password as string,
options: {
emailRedirectTo: `${origin}/auth/callback`,
emailRedirectTo: `${origin}/auth/confirm`,
data: { username: username },
},
});

if (error) {
console.log(error);
return { error: error.message };
}
if (session) {
if (session || user?.role !== 'authenticated') {
return { error: 'Email already exists' };
}
return { error: null };
return { user_id: user?.id, error: null };
};

export const AuthSignIn = async () => {
const origin = headers().get("origin");
const gmail = cookies()?.get('email')?.value || '';
const supabase = createClient();
const { data, error } = await supabase.auth.signInWithOAuth({
provider: 'google',
options: {
redirectTo: `${origin}/auth/callback`,
queryParams: {
include_granted_scopes: 'true',
access_type: 'offline',
prompt: 'select_account',
login_hint: gmail,
},
},
});
if (error) return { error: error.message, url: null };
if (data.url) return { error: null, url: data.url };
return { error: 'Error signing in', url: null };
const origin = headers().get("origin");
const gmail = cookies()?.get('email')?.value || '';

const supabase = createClient();
const { data, error } = await supabase.auth.signInWithOAuth({
provider: 'google',
options: {
redirectTo: `${origin}/auth/callback`,
queryParams: {
include_granted_scopes: 'true',
access_type: 'offline',
prompt: 'select_account',
login_hint: gmail,
},
},
});
if (error) return { error: error.message, url: null };
if (data.url) return { error: null, url: data.url };
return { error: 'Error signing in', url: null };
}

export async function usernameExisits(username: string): Promise<boolean> {
Expand Down
1 change: 1 addition & 0 deletions src/app/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export async function GET(req: NextRequest, res: NextResponse) {
console.log('x ',error);
}
}

return NextResponse.redirect(url);
}
Loading

0 comments on commit 4926a31

Please sign in to comment.