Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: messenger clone added on MERN stack #257

Closed
Closed
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
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions Messenger_Clone/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL=
NEXTAUTH_SECRET=

NEXT_PUBLIC_PUSHER_APP_KEY=
PUSHER_APP_ID=
PUSHER_SECRET=

NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME=

GITHUB_ID=
GITHUB_SECRET=

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
3 changes: 3 additions & 0 deletions Messenger_Clone/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions Messenger_Clone/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
4 changes: 4 additions & 0 deletions Messenger_Clone/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
21 changes: 21 additions & 0 deletions Messenger_Clone/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Antonio Erdeljac

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
76 changes: 76 additions & 0 deletions Messenger_Clone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Messenger Clone
Fully responsive messenger clone on MERN Stack

#### Tech Stack
-Next.js 13
-React
-Tailwind
-Prisma
-MongoDB
-NextAuth
-Pusher


#### Demo
Check out the demo of the Messenger clone on YouTube: [Messenger Clone Demo](https://www.youtube.com/watch?v=HHVamziJsWs)

### Prerequisites

**Node version 14.x**

### Clone the Repository
```bash
git clone https://github.com/Kritika30032002/ReactCreations.git
```

### Navigate to the Project Directory
```bash
cd ReactCreations\Messenger_Clone
```

### Install packages

```shell
npm i
```

### Setup .env file


```js
DATABASE_URL=
NEXTAUTH_SECRET=

NEXT_PUBLIC_PUSHER_APP_KEY=
PUSHER_APP_ID=
PUSHER_SECRET=

NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME=

GITHUB_ID=
GITHUB_SECRET=

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
```

### Setup Prisma

```shell
npx prisma db push

```

### Start the app

```shell
npm run dev
```

## Available commands

Running commands with npm `npm run [command]`

| command | description |
| :-------------- | :--------------------------------------- |
| `dev` | Starts a development instance of the app |
214 changes: 214 additions & 0 deletions Messenger_Clone/app/(site)/components/AuthForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
'use client';

import axios from "axios";
import { signIn, useSession } from 'next-auth/react';
import { useCallback, useEffect, useState } from 'react';
import { BsGithub, BsGoogle } from 'react-icons/bs';
import { FieldValues, SubmitHandler, useForm } from 'react-hook-form';
import { useRouter } from "next/navigation";

import Input from "@/app/components/inputs/Input";
import AuthSocialButton from './AuthSocialButton';
import Button from "@/app/components/Button";
import { toast } from "react-hot-toast";

type Variant = 'LOGIN' | 'REGISTER';

const AuthForm = () => {
const session = useSession();
const router = useRouter();
const [variant, setVariant] = useState<Variant>('LOGIN');
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
if (session?.status === 'authenticated') {
router.push('/conversations')
}
}, [session?.status, router]);

const toggleVariant = useCallback(() => {
if (variant === 'LOGIN') {
setVariant('REGISTER');
} else {
setVariant('LOGIN');
}
}, [variant]);

const {
register,
handleSubmit,
formState: {
errors,
}
} = useForm<FieldValues>({
defaultValues: {
name: '',
email: '',
password: ''
}
});

const onSubmit: SubmitHandler<FieldValues> = (data) => {
setIsLoading(true);

if (variant === 'REGISTER') {
axios.post('/api/register', data)
.then(() => signIn('credentials', {
...data,
redirect: false,
}))
.then((callback) => {
if (callback?.error) {
toast.error('Invalid credentials!');
}

if (callback?.ok) {
router.push('/conversations')
}
})
.catch(() => toast.error('Something went wrong!'))
.finally(() => setIsLoading(false))
}

if (variant === 'LOGIN') {
signIn('credentials', {
...data,
redirect: false
})
.then((callback) => {
if (callback?.error) {
toast.error('Invalid credentials!');
}

if (callback?.ok) {
router.push('/conversations')
}
})
.finally(() => setIsLoading(false))
}
}

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

signIn(action, { redirect: false })
.then((callback) => {
if (callback?.error) {
toast.error('Invalid credentials!');
}

if (callback?.ok) {
router.push('/conversations')
}
})
.finally(() => setIsLoading(false));
}

return (
<div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
<div
className="
bg-white
px-4
py-8
shadow
sm:rounded-lg
sm:px-10
"
>
<form
className="space-y-6"
onSubmit={handleSubmit(onSubmit)}
>
{variant === 'REGISTER' && (
<Input
disabled={isLoading}
register={register}
errors={errors}
required
id="name"
label="Name"
/>
)}
<Input
disabled={isLoading}
register={register}
errors={errors}
required
id="email"
label="Email address"
type="email"
/>
<Input
disabled={isLoading}
register={register}
errors={errors}
required
id="password"
label="Password"
type="password"
/>
<div>
<Button disabled={isLoading} fullWidth type="submit">
{variant === 'LOGIN' ? 'Sign in' : 'Register'}
</Button>
</div>
</form>

<div className="mt-6">
<div className="relative">
<div
className="
absolute
inset-0
flex
items-center
"
>
<div className="w-full border-t border-gray-300" />
</div>
<div className="relative flex justify-center text-sm">
<span className="bg-white px-2 text-gray-500">
Or continue with
</span>
</div>
</div>

<div className="mt-6 flex gap-2">
<AuthSocialButton
icon={BsGithub}
onClick={() => socialAction('github')}
/>
<AuthSocialButton
icon={BsGoogle}
onClick={() => socialAction('google')}
/>
</div>
</div>
<div
className="
flex
gap-2
justify-center
text-sm
mt-6
px-2
text-gray-500
"
>
<div>
{variant === 'LOGIN' ? 'New to Messenger?' : 'Already have an account?'}
</div>
<div
onClick={toggleVariant}
className="underline cursor-pointer"
>
{variant === 'LOGIN' ? 'Create an account' : 'Login'}
</div>
</div>
</div>
</div>
);
}

export default AuthForm;
Loading