Skip to content

Commit

Permalink
social login implemented with google and github
Browse files Browse the repository at this point in the history
  • Loading branch information
17coincooker committed Sep 9, 2023
1 parent f6e49be commit ad6a2dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
32 changes: 29 additions & 3 deletions app/(site)/components/AuthForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import AuthSocialButton from './AuthSocialButton';
import { BsGithub, BsGoogle } from 'react-icons/bs';
import axios from 'axios';
import { toast } from 'react-hot-toast';
import { signIn } from 'next-auth/react';

type Variant = 'LOGIN' | 'REGISTER';

Expand Down Expand Up @@ -38,7 +39,7 @@ const AuthForm = () => {
const errorCode = err.response.status;

if (errorCode === 400) {
toast.error('Please fill all fields!');
toast.error('Please enter your name, email and password!');
} else if (errorCode === 409) {
toast.error('Email already exists!');
} else {
Expand All @@ -49,14 +50,39 @@ const AuthForm = () => {
}

if (variant === 'LOGIN') {
// NextAuth SignIn
signIn('credentials', {
...data,
redirect: false,
})
.then((res) => {
if (res?.error) {
toast.error(res.error);
}

if (res?.ok && !res?.error) {
toast.success('Logged in!');
}
})
.finally(() => setIsLoading(false));
}
};

const socialAction = (action: string) => {
setIsLoading(true);

// NextAuth Social SignIn
signIn(action, {
redirect: false,
})
.then((res) => {
if (res?.error) {
toast.error(res.error);
}

if (res?.ok && !res?.error) {
toast.success('Logged in!');
}
})
.finally(() => setIsLoading(false));
};

return (
Expand Down
2 changes: 1 addition & 1 deletion app/api/auth/[...nextAuth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const authOptions: AuthOptions = {
async authorize(credentials) {
// check if user details are passed
if (!credentials?.email || !credentials?.password) {
throw new Error('Invalid credentials');
throw new Error('Please enter your credentials');
}

// find user in database
Expand Down
4 changes: 4 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@
- prisma
- axios
- react-hot-toast

## ToDos

- [ ] Add twitter login

0 comments on commit ad6a2dc

Please sign in to comment.