Skip to content
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
Binary file modified bun.lockb
Binary file not shown.
16 changes: 8 additions & 8 deletions src/actions/auth/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

import { signIn } from '@/auth';
import { redirect } from 'next/navigation';

import { OAuthAccountNotLinked } from '@auth/core/errors';
import { deleteUserAccount } from '@/db/query/User';
import { revalidatePath } from 'next/cache';

// =============================== Oauth Login ===============================
export async function oAuthLogin(provider: string) {
let user = '/';
try {
user = await signIn(provider, { redirect: false });
console.log('user', user);
await signIn(provider);
} catch (error) {
console.log('error', error);
console.log('Error------', error);
if (error instanceof OAuthAccountNotLinked) {
redirect('/error?error=OAuthAccountNotLinked');
} else {
throw error;
}
}
// console.log(user);

if (user) redirect(user);
}

// =============================== Oauth Remove ===============================
Expand Down
19 changes: 18 additions & 1 deletion src/app/(auth)/sign-in/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import Link from 'next/link';
import SignInForm from './SignInForm';
import { GithubSignIn, GoogleSignIn } from '@/components/AuthButton';
import { redirect } from 'next/navigation';
import { auth } from '@/auth';

export default function SignIn() {
export default async function SignIn({
searchParams,
}: {
searchParams?: {
error?: string;
};
}) {
const error = searchParams?.error || '';
if (error) {
redirect(`/error?error=${error}`);
}
const session = await auth();
const user = session?.user;
if (user) {
redirect('/profile');
}
return (
<div className='mx-auto flex min-h-screen flex-col items-center justify-center'>
<div className='mx-auto flex flex-col gap-2 rounded-lg p-8 shadow-lg shadow-black dark:shadow-white'>
Expand Down
31 changes: 2 additions & 29 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import NextAuth, { AuthError } from 'next-auth';
// import { OAuthAccountNotLinked } from '@auth/core/errors';
import Google from 'next-auth/providers/google';
import Github from 'next-auth/providers/github';
import Credentials from 'next-auth/providers/credentials';
import { DrizzleAdapter } from '@auth/drizzle-adapter';
import { db } from '@/db';
import {
getUserById,
getUserByProviderAccountId,
loginUser,
} from './db/query/User';
import { getUserById, loginUser } from './db/query/User';
import bcrypt from 'bcryptjs';
import { encode, decode } from 'next-auth/jwt';
import { cookies } from 'next/headers';

class InvalidCredentialsError extends AuthError {
code = 'invalid-credentials';
Expand Down Expand Up @@ -71,35 +65,14 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
}),
],
callbacks: {
async signIn({ account }) {
const cookieStore = cookies();
const session = cookieStore.has('authjs.session-token');
// If not logged in, let user login
if (!session) {
return true;
}
// If already logged in, and try to connect another account, throw error if already linked
if (account?.provider === 'github' || account?.provider === 'google') {
// check if user already exists with this account.providerAccountId
const existingUser = await getUserByProviderAccountId(
account?.providerAccountId as string,
);
if (existingUser) {
return '/error?error=OAuthAccountNotLinked';
} else {
return true;
}
}
return true;
},
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;
const paths = ['/profile', '/dashboard'];
const isProtected = paths.some((path) =>
nextUrl.pathname.startsWith(path),
);

const publicPath = ['/sign-in', '/sign-up'];
const publicPath = ['/sign-up'];
const isPublic = publicPath.some((path) =>
nextUrl.pathname.startsWith(path),
);
Expand Down
22 changes: 11 additions & 11 deletions src/db/query/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ export const getUserById = async (id: string) => {
return result;
};

export const getUserByProviderAccountId = async (id: string) => {
const result = await db.query.accounts.findFirst({
where: (accounts: { providerAccountId: any }, { eq }: any) =>
eq(accounts.providerAccountId, id),
columns: {
// Include only fields you want from users table, excluding password
userId: true,
},
});
return result;
};
// export const getUserByProviderAccountId = async (id: string) => {
// const result = await db.query.accounts.findFirst({
// where: (accounts: { providerAccountId: any }, { eq }: any) =>
// eq(accounts.providerAccountId, id),
// columns: {
// // Include only fields you want from users table, excluding password
// userId: true,
// },
// });
// return result;
// };

export const createUser = async (
name: string,
Expand Down