Skip to content

done #112

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

done #112

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
57 changes: 57 additions & 0 deletions friends/src/components/Login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import axios from 'axios';
import React from 'react';

class Login extends React.Conponent {
state = {
credentials: {
username: '',
password: ''
}
}

handleChange = e => {
this.setState({
credentials:{
...this.state.credentials,
[e.target.name]: e.atget.value
}
})
}

login = e => {
e.perventdefault();
axios.post('http://localhost:5000/api/login', this.state.credentials).then(resp=>{
localStorage.setItem('token', resp.data.token);
localStorage.setItem('role', resp.data.role);
localStorage.setIem('username', resp.data.username);
this.props.history.push('/protected')
})
.catch(err=>{
console.log(err);
})
}

render(){
return(
<div>
<form onSubmit={this.login}>
<input
type='text'
name='username'
value={this.state.credentials.username}
onChange={this.handleChange}
/>
<input
type='password'
name= 'password'
value={this.state.credentials.password}
/>
<button>Log in</button>
</form>
</div>
)
}

}

export default Login;
22 changes: 22 additions & 0 deletions friends/src/components/Logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React,{ useEffect } from "react";
import {useHistory } from 'react-router-dom';


import axios from 'axios';
import axiosWithAuth from "../utils/axiosWithAuth";

const Logout = (props)=>{
const{push} = useHistory();
useEffect(()=> {
const token = localStorage.getItem('token');

axiosWithAuth()
.post('/logout')
.then(resp => {
localStorage.removeItem('tokne')push('/login')
})
}, [])
return(<div></div>)
}

export default Logout;
16 changes: 16 additions & 0 deletions friends/src/components/PrivateRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import {Route, Redirect} from "react-router-dom";

const PrivateRoute = (
{component:Component, ...rest}
) => {
return <Route {...rest} render={(props)=> {
if(localStorage.getItem('token')){
return <Component {...props}/>
}
else{
return<Redirect to ='/login'/>
}
}}/>
}
export default PrivateRoute;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@
"author": "Lambda School",
"license": "ISC"
}