Created
November 1, 2022 15:12
-
-
Save Josh4324/857a3b4975098a7ce0e7253beba9a951 to your computer and use it in GitHub Desktop.
login.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useRef } from "react"; | |
import bcrypt from "bcryptjs"; | |
import { NotificationManager } from "react-notifications"; | |
import Link from "next/link"; | |
export default function Login() { | |
const emailRef = useRef(); | |
const passwordRef = useRef(); | |
const log = () => { | |
if (emailRef.current.value === "" || passwordRef.current.value === "") { | |
return NotificationManager.error("Please enter your email or password"); | |
} | |
const options = { | |
method: "POST", | |
body: JSON.stringify({ email: emailRef.current.value }), | |
}; | |
NotificationManager.info("Loading.......", "Info"); | |
(async () => { | |
try { | |
const response = await fetch("/api/login", options); | |
const data = await response.json(); | |
if (data) { | |
bcrypt.compare( | |
passwordRef.current.value, | |
data[0].password, | |
(err, result) => { | |
if (result === true) { | |
NotificationManager.success( | |
"Login was successfully", | |
"Success" | |
); | |
localStorage.setItem("cloud-user", data[0].id); | |
localStorage.setItem("cloud-email", emailRef.current.value); | |
window.location.href = "/"; | |
} else { | |
NotificationManager.error("Wrong email or password", "Error"); | |
} | |
} | |
); | |
} | |
} catch (err) { | |
console.log(err); | |
} | |
})(); | |
}; | |
return ( | |
<div> | |
<div className="form"> | |
<h2 className="white">Login</h2> | |
<div> | |
<input className="input" ref={emailRef} placeholder="Enter Email" /> | |
</div> | |
<div> | |
<input | |
className="input" | |
ref={passwordRef} | |
placeholder="Enter Password" | |
type="password" | |
/> | |
</div> | |
<div> | |
{/* eslint-disable-next-line @next/next/no-html-link-for-pages */} | |
<Link href="/signup">Signup</Link> | |
</div> | |
<button className="but" onClick={log}> | |
Login | |
</button> | |
</div> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment