1
0
Fork 0

Somewhat there figuring login status

This commit is contained in:
Amarpreet Minhas 2019-10-16 20:12:14 -04:00
parent 7c5c6f5d98
commit c7e2bb6d74
3 changed files with 48 additions and 14 deletions

View file

@ -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 })
};

View file

@ -8,13 +8,41 @@ class AuthMenu extends Component {
constructor (props) { constructor (props) {
super(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 = { this.state = {
user_authed: false, user_authed: false,
where_in_auth_menu: "requestUsername", where_in_auth_menu: "requestUsername",
username: "", username: "",
password: "", 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) 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);
}) })

View file

@ -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':
if (action.payload) {
var data = JSON.parse(atob(action.payload.data.split('.')[1]))
return {...state, ...{ return {...state, ...{
username: action.payload.data.username, user_logged_in: true,
user_logged_in: true username: data.username,
}} }}
}
else return;
default: default:
return state; return state;
} }