Skip to content
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

Move Towards Redux #304

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nishant23122000
Copy link

Fixes #273

Description

So as I suggested a new feature of redux, I implement reducer, the action of only user state because my target is to connect frontend with backend so I focus only on authentication side, we can add more reducer and action in future,

when the user login, we will send back the token in response from the backend,

res.status(200).json({
      success: true,
      data: {
        ...existingUser._doc,
        password: '',
        passwordConfirm: "",
        token: `Bearer ${token}`
        -----------------------
      },
    });

At frontend side, we create a cookie with the help of js-cookie package,

  const res = await axios.post('/auth/signin', userData);

        // set cookie in browser
        Cookies.set("token", res.data.data.token);

we will check repeatedly whether the user token expired or not by decoding the token with the help of jwt-decode package and change the state according to the result,

 const tokenData = jwt_decode(Cookies.get('token').split(' ')[1]);
        const currentTime = Date.now();
        const tokenExpireTime = new Date(0).setUTCSeconds(tokenData.exp);

        // to check wheather token is valid or load
        if (currentTime < tokenExpireTime)
            dispatch({ type: IS_LOGGEDIN })
        else
            dispatch({ type: LOGOUT_USER })

Also, I create a logout button that will remove the cookie from the browser when the user clicks on this button.

 Cookies.remove("token");
    dispatch({ type: LOGOUT_USER })

There are many things to improve like handle an error with proper UI. but I want suggestion from you guys whether there is something to change or not,

Screenshots

Addition Information (if any)

@gauravdas014 gauravdas014 requested a review from pkspyder007 April 6, 2021 16:50
@gauravdas014
Copy link
Collaborator

@nishant23122000 why are checks failing? Make sure there is no warning in the console

@nishant23122000
Copy link
Author

nishant23122000 commented Apr 6, 2021

@nishant23122000 why are checks failing? Make sure there is no warning in the console

Hey, sorry I made a tiny mistake, but now check it, it's solved😎

@gauravdas014
Copy link
Collaborator

@nishant23122000 your PR will be reviewed by @pkspyder007 and @nanda-mik

@nanda-mik
Copy link
Collaborator

Reducers and stores are implemented correctly. @pkspyder007 Please review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

moving on redux
3 participants