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) {
|
if (response.status === 401) {
|
||||||
console.log("Please check your credentials")
|
dispatch({ type: 'USER_LOGIN', payload: null })
|
||||||
}
|
}
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
dispatch({ type: 'USER_LOGIN', payload: response })
|
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) {
|
constructor (props) {
|
||||||
super(props)
|
super(props)
|
||||||
|
|
||||||
this.state = {
|
const cookies = new Cookies();
|
||||||
user_authed: false,
|
const datacookie = cookies.get('DataCookie');
|
||||||
where_in_auth_menu: "requestUsername",
|
|
||||||
username: "",
|
if (datacookie) {
|
||||||
password: "",
|
const data = atob(datacookie.split('.')[1])
|
||||||
email: ""
|
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: "",
|
||||||
|
exp: null,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
this.state = {
|
||||||
|
user_authed: false,
|
||||||
|
where_in_auth_menu: "requestUsername",
|
||||||
|
username: "",
|
||||||
|
password: "",
|
||||||
|
email: "",
|
||||||
|
exp: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
this.handleInputChange = this.handleInputChange.bind(this)
|
this.handleInputChange = this.handleInputChange.bind(this)
|
||||||
|
|
||||||
|
@ -27,7 +55,6 @@ class AuthMenu extends Component {
|
||||||
this.handleCreateAccount = this.handleCreateAccount.bind(this)
|
this.handleCreateAccount = this.handleCreateAccount.bind(this)
|
||||||
|
|
||||||
this.authMenu = this.authMenu.bind(this)
|
this.authMenu = this.authMenu.bind(this)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,7 +90,6 @@ class AuthMenu extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleLogin() {
|
handleLogin() {
|
||||||
const cookies = new Cookies();
|
|
||||||
this.props.userLogin(this.state.username, this.state.password)
|
this.props.userLogin(this.state.username, this.state.password)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
this.setState(state => ({
|
this.setState(state => ({
|
||||||
|
@ -72,7 +98,6 @@ class AuthMenu extends Component {
|
||||||
this.props.close()
|
this.props.close()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.then(console.log(cookies.getAll()))
|
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
})
|
})
|
||||||
|
|
|
@ -8,10 +8,14 @@ const initialState = {
|
||||||
export default (state = initialState, action) => {
|
export default (state = initialState, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'USER_LOGIN':
|
case 'USER_LOGIN':
|
||||||
return {...state, ...{
|
if (action.payload) {
|
||||||
username: action.payload.data.username,
|
var data = JSON.parse(atob(action.payload.data.split('.')[1]))
|
||||||
user_logged_in: true
|
return {...state, ...{
|
||||||
}}
|
user_logged_in: true,
|
||||||
|
username: data.username,
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
else return;
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue