Skip to content

Commit c91be42

Browse files
Merge pull request #4 from patelvivekdev/main
merge main to dev
2 parents 60835e0 + 9e656cb commit c91be42

File tree

5 files changed

+39
-49
lines changed

5 files changed

+39
-49
lines changed

bun.lockb

0 Bytes
Binary file not shown.

src/actions/auth/oauth.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
import { signIn } from '@/auth';
44
import { redirect } from 'next/navigation';
5-
5+
import { OAuthAccountNotLinked } from '@auth/core/errors';
66
import { deleteUserAccount } from '@/db/query/User';
77
import { revalidatePath } from 'next/cache';
88

99
// =============================== Oauth Login ===============================
1010
export async function oAuthLogin(provider: string) {
11-
let user = '/';
1211
try {
13-
user = await signIn(provider, { redirect: false });
14-
console.log('user', user);
12+
await signIn(provider);
1513
} catch (error) {
16-
console.log('error', error);
14+
console.log('Error------', error);
15+
if (error instanceof OAuthAccountNotLinked) {
16+
redirect('/error?error=OAuthAccountNotLinked');
17+
} else {
18+
throw error;
19+
}
1720
}
18-
// console.log(user);
19-
20-
if (user) redirect(user);
2121
}
2222

2323
// =============================== Oauth Remove ===============================

src/app/(auth)/sign-in/page.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
import Link from 'next/link';
22
import SignInForm from './SignInForm';
33
import { GithubSignIn, GoogleSignIn } from '@/components/AuthButton';
4+
import { redirect } from 'next/navigation';
5+
import { auth } from '@/auth';
46

5-
export default function SignIn() {
7+
export default async function SignIn({
8+
searchParams,
9+
}: {
10+
searchParams?: {
11+
error?: string;
12+
};
13+
}) {
14+
const error = searchParams?.error || '';
15+
if (error) {
16+
redirect(`/error?error=${error}`);
17+
}
18+
const session = await auth();
19+
const user = session?.user;
20+
if (user) {
21+
redirect('/profile');
22+
}
623
return (
724
<div className='mx-auto flex min-h-screen flex-col items-center justify-center'>
825
<div className='mx-auto flex flex-col gap-2 rounded-lg p-8 shadow-lg shadow-black dark:shadow-white'>

src/auth.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import NextAuth, { AuthError } from 'next-auth';
2-
// import { OAuthAccountNotLinked } from '@auth/core/errors';
32
import Google from 'next-auth/providers/google';
43
import Github from 'next-auth/providers/github';
54
import Credentials from 'next-auth/providers/credentials';
65
import { DrizzleAdapter } from '@auth/drizzle-adapter';
76
import { db } from '@/db';
8-
import {
9-
getUserById,
10-
getUserByProviderAccountId,
11-
loginUser,
12-
} from './db/query/User';
7+
import { getUserById, loginUser } from './db/query/User';
138
import bcrypt from 'bcryptjs';
149
import { encode, decode } from 'next-auth/jwt';
15-
import { cookies } from 'next/headers';
1610

1711
class InvalidCredentialsError extends AuthError {
1812
code = 'invalid-credentials';
@@ -71,35 +65,14 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
7165
}),
7266
],
7367
callbacks: {
74-
async signIn({ account }) {
75-
const cookieStore = cookies();
76-
const session = cookieStore.has('authjs.session-token');
77-
// If not logged in, let user login
78-
if (!session) {
79-
return true;
80-
}
81-
// If already logged in, and try to connect another account, throw error if already linked
82-
if (account?.provider === 'github' || account?.provider === 'google') {
83-
// check if user already exists with this account.providerAccountId
84-
const existingUser = await getUserByProviderAccountId(
85-
account?.providerAccountId as string,
86-
);
87-
if (existingUser) {
88-
return '/error?error=OAuthAccountNotLinked';
89-
} else {
90-
return true;
91-
}
92-
}
93-
return true;
94-
},
9568
authorized({ auth, request: { nextUrl } }) {
9669
const isLoggedIn = !!auth?.user;
9770
const paths = ['/profile', '/dashboard'];
9871
const isProtected = paths.some((path) =>
9972
nextUrl.pathname.startsWith(path),
10073
);
10174

102-
const publicPath = ['/sign-in', '/sign-up'];
75+
const publicPath = ['/sign-up'];
10376
const isPublic = publicPath.some((path) =>
10477
nextUrl.pathname.startsWith(path),
10578
);

src/db/query/User.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ export const getUserById = async (id: string) => {
3939
return result;
4040
};
4141

42-
export const getUserByProviderAccountId = async (id: string) => {
43-
const result = await db.query.accounts.findFirst({
44-
where: (accounts: { providerAccountId: any }, { eq }: any) =>
45-
eq(accounts.providerAccountId, id),
46-
columns: {
47-
// Include only fields you want from users table, excluding password
48-
userId: true,
49-
},
50-
});
51-
return result;
52-
};
42+
// export const getUserByProviderAccountId = async (id: string) => {
43+
// const result = await db.query.accounts.findFirst({
44+
// where: (accounts: { providerAccountId: any }, { eq }: any) =>
45+
// eq(accounts.providerAccountId, id),
46+
// columns: {
47+
// // Include only fields you want from users table, excluding password
48+
// userId: true,
49+
// },
50+
// });
51+
// return result;
52+
// };
5353

5454
export const createUser = async (
5555
name: string,

0 commit comments

Comments
 (0)