Somewhat there figuring login status
This commit is contained in:
parent
7c5c6f5d98
commit
c7e2bb6d74
3 changed files with 48 additions and 14 deletions
|
@ -25,9 +25,14 @@ export const userLogin = (username, password) => async (dispatch) => {
|
|||
}
|
||||
)
|
||||
if (response.status === 401) {
|
||||
console.log("Please check your credentials")
|
||||
dispatch({ type: 'USER_LOGIN', payload: null })
|
||||
}
|
||||
if (response.status === 200) {
|
||||
dispatch({ type: 'USER_LOGIN', payload: response })
|
||||
}
|
||||
};
|
||||
|
||||
export const checkJWT = (jwt) => async (dispatch) => {
|
||||
|
||||
dispatch({ type: 'FETCH_USER', payload: null })
|
||||
};
|
||||
|
|
|
@ -8,13 +8,41 @@ class AuthMenu extends Component {
|
|||
constructor (props) {
|
||||
super(props)
|
||||
|
||||
const cookies = new Cookies();
|
||||
const datacookie = cookies.get('DataCookie');
|
||||
|
||||
if (datacookie) {
|
||||
const data = atob(datacookie.split('.')[1])
|
||||
if (data.exp < Date.now()) {
|
||||
this.state = {
|
||||
user_authed: true,
|
||||
where_in_auth_menu: "loggedIn",
|
||||
username: data.username,
|
||||
exp: data.exp,
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.state = {
|
||||
user_authed: false,
|
||||
where_in_auth_menu: "requestUsername",
|
||||
username: "",
|
||||
password: "",
|
||||
email: ""
|
||||
email: "",
|
||||
exp: null,
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.state = {
|
||||
user_authed: false,
|
||||
where_in_auth_menu: "requestUsername",
|
||||
username: "",
|
||||
password: "",
|
||||
email: "",
|
||||
exp: null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this.handleInputChange = this.handleInputChange.bind(this)
|
||||
|
||||
|
@ -27,7 +55,6 @@ class AuthMenu extends Component {
|
|||
this.handleCreateAccount = this.handleCreateAccount.bind(this)
|
||||
|
||||
this.authMenu = this.authMenu.bind(this)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -63,7 +90,6 @@ class AuthMenu extends Component {
|
|||
}
|
||||
|
||||
handleLogin() {
|
||||
const cookies = new Cookies();
|
||||
this.props.userLogin(this.state.username, this.state.password)
|
||||
.then(res => {
|
||||
this.setState(state => ({
|
||||
|
@ -72,7 +98,6 @@ class AuthMenu extends Component {
|
|||
this.props.close()
|
||||
}
|
||||
)
|
||||
.then(console.log(cookies.getAll()))
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
})
|
||||
|
|
|
@ -8,10 +8,14 @@ const initialState = {
|
|||
export default (state = initialState, action) => {
|
||||
switch (action.type) {
|
||||
case 'USER_LOGIN':
|
||||
if (action.payload) {
|
||||
var data = JSON.parse(atob(action.payload.data.split('.')[1]))
|
||||
return {...state, ...{
|
||||
username: action.payload.data.username,
|
||||
user_logged_in: true
|
||||
user_logged_in: true,
|
||||
username: data.username,
|
||||
}}
|
||||
}
|
||||
else return;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
|
Reference in a new issue