Skip to content

Commit

Permalink
feat(auth): fetch-account and is-authenticated utils
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Dec 12, 2023
1 parent 41ca85c commit 29a71e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ const getUserId = async () => {
return userQuery.data.user.id;
};

export const fetchAccount = async () => {
const supabase = createSupabaseServerClient(cookies());

const accountQuery = await supabase
.from('account')
.select('*')
.eq('user_id', await getUserId())
.single();

if (accountQuery.error) {
throw new Error(accountQuery.error.message);
}

return accountQuery.data;
};

export const fetchAccountAICredits = async () => {
const supabase = createSupabaseServerClient(cookies());

Expand Down
13 changes: 13 additions & 0 deletions lib/services/supabase/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import type { NextRequest } from 'next/server';

import { env } from '@/env.mjs';
import { createServerClient } from '@supabase/ssr';
import { cookies } from 'next/headers';
import { NextResponse } from 'next/server';

import { createSupabaseServerClient } from './server';

export const getSupabaseAuthSession = async (request: NextRequest) => {
let response = NextResponse.next({
request: {
Expand Down Expand Up @@ -61,3 +64,13 @@ export const getSupabaseAuthSession = async (request: NextRequest) => {

return { authSession, response };
};

export const getIsAuthenticated = async () => {
const supabase = createSupabaseServerClient(cookies());

const {
data: { session },
} = await supabase.auth.getSession();

return session !== null;
};

0 comments on commit 29a71e5

Please sign in to comment.