Skip to content

Commit 007d34a

Browse files
login actions and login alert notification added
1 parent 852d776 commit 007d34a

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Fullstack-MERN-Blogsite
22
Fullstack Blogsite built using React as a frontend , express js (backend framework) and MongoDB as a database.Also, implemented various features like form validation ,user authentication(signup and login functionalities) ,separate user profiles, reset password, forgot password and much more.
33

4-
commit History
4+
Step By Step Build:
5+
Entire commit History
6+
57
```
68
| a7241c556cc959b383d3d578e3dd0d95329fcaf5 | Fullstack Mern blogsite |
79
| befa63adffe6552c0dcde0a6f04e5837fed13a9d | Create README.md |
@@ -17,4 +19,6 @@ commit History
1719
| ac66b43e13aca8e63c381b62a6761101224a8b63 | login and register page added and navbar updated with respective links |
1820
| c4f19452700c40eb2f5dda86548ad8f12b00637b | readme updated |
1921
| 00236e5aa973531e157588c809145b3dafe1ba7d | signup form created |
22+
| 852d776b09c675c64bca6a6009f50cbdc37aab90 | register actions and register alert notification added |
23+
2024
```

client/actions/authentication.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,19 @@ export const signup = (user) => {
1414
return response.json();
1515
})
1616
.catch(error => console.log(error));
17+
};
18+
19+
export const login = (user) => {
20+
return fetch(`${API}/api/login`, {
21+
method: 'POST',
22+
headers: {
23+
Accept: 'application/json',
24+
'Content-Type': 'application/json'
25+
},
26+
body: JSON.stringify(user)
27+
})
28+
.then(response => {
29+
return response.json();
30+
})
31+
.catch(error => console.log(error));
1732
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React,{useState} from 'react';
2+
import {login} from "../../actions/authentication.js";
3+
import Router from "next/router";
4+
5+
const LoginAuth = () => {
6+
7+
const [info,setInfo] = useState({email:"",password:"",error:"",loading:false,message:"",showForm:true});
8+
9+
const {email,password,error,loading,message,showForm} = info
10+
11+
const handleSubmit = event => {
12+
event.preventDefault();
13+
// console.table({ name, email, password, error, loading, message, showForm });
14+
setInfo({ ...info, loading:true, error:false });
15+
const user = {email, password };
16+
17+
login(user).then(data => {
18+
19+
// console.log("data.error",data.name)
20+
21+
if (data.error) {
22+
setInfo({ ...info, error: data.error, loading: false });
23+
} else {
24+
// user token to cookie
25+
// user information to localstorage
26+
// authenticate the user
27+
Router.push(`/`)
28+
}
29+
});
30+
};
31+
32+
const handleChange= name =>(event)=>{
33+
setInfo({...info,error:false,[name]: event.target.value});
34+
}
35+
36+
const showLoading = () =>(loading ? <div className= "alert alert-success">Loading.....</div> : "");
37+
const showError = () =>(error ? <div className= "alert alert-danger">{error}</div> : "");
38+
39+
const showMessage = () =>(message ? <div className= "alert alert-primary">{message}</div> : "");
40+
41+
const loginForm=()=>{
42+
return (
43+
44+
<form onSubmit={handleSubmit}>
45+
46+
{/* Email */}
47+
<div className="form-group">
48+
<input type="email" value={email} className="form-control" onChange={handleChange("email")} placeholder="Enter your @Email address"/>
49+
</div>
50+
{/* Password */}
51+
<div className="form-group">
52+
<input type="password" value={password} className="form-control" onChange={handleChange("password")} placeholder="New password"/>
53+
</div>
54+
{/* <div className = */}
55+
<div className="col text-center">
56+
<button className="btn btn-info pt-3 pb-3 pl-5 pr-5">Login</button>
57+
</div>
58+
</form>
59+
)
60+
};
61+
62+
return (
63+
<>
64+
{showError()}
65+
{showLoading()}
66+
{showMessage()}
67+
{showForm && loginForm()}
68+
</>
69+
)
70+
}
71+
72+
export default LoginAuth;

client/pages/login.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import Layout from "../components/Layout";
1+
import React from "react";
2+
import Layout from "../components/Layout";
3+
import LoginAuth from "../components/authentication/LoginAuth";
24
import Link from "next/link";
35

4-
56
const Login =() =>{
67
return(
7-
<Layout>
8+
<Layout>
89
<Link href="/"><a>Home</a></Link>
9-
<br/>
10-
<h2>Login page</h2>
10+
<h3 className="text-center pt-3 pb-5">Login</h3>
11+
<div className="row">
12+
<div className="col-md-4 offset-md-4">
13+
<LoginAuth/>
14+
</div>
15+
</div>
1116
</Layout>
1217
)
1318
}

0 commit comments

Comments
 (0)