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

Feat: Issue#30 protected page and Persistent Token #34

Merged

Conversation

mtreacy002
Copy link
Member

@mtreacy002 mtreacy002 commented Jul 1, 2020

Description

Add ProtectedPage functionality to separate Public vs Private/Members-only pages

Fixes #30

Type of Change:

  • Code
  • User Interface

Code/Quality Assurance Only

  • New feature (non-breaking change which adds functionality pre-approved by mentors)

How Has This Been Tested?

  • by running NPM test to make sure all existing tests still pass
  • testing manually by runnning the app and confirm the followings:
    • on initial landing page, the navbar shows Register and Login options
    • trying to access 'My Space' page (which is a private member page) when user not login, resulted in redirection to the login page, which is the expected behaviour.
    • after successful login, user is redirected to homepage and navbar is showing Logout option
    • while user logged-in, 'My Space' page is accessible directly (without redirection to login page)
    • after user logout, user gets redirected to the homepage and the navbar tab back to showing Register and Login options. 'My Space' page also no longer accessible.

Updated gif link

gif showing protected route

Checklist:

  • My PR follows the style guidelines of this project
  • I have performed a self-review of my own code or materials

Code/Quality Assurance Only

  • My changes generate no new warnings

@mtreacy002 mtreacy002 self-assigned this Jul 1, 2020
@mtreacy002 mtreacy002 added Category: Coding Changes to code base or refactored code that doesn't fix a bug. Category: User Interface Improvements or additions to design. Program: GSOC Related to work completed during the Google Summer of Code Program. labels Jul 1, 2020
@mtreacy002 mtreacy002 modified the milestone: GSoC: Coding Phase I Jul 1, 2020
@mtreacy002
Copy link
Member Author

Updatte @anitab-org/bridgeintech-maintainers . I've just pushed the latest code. Please give your feedback. In the meantime I'll start backend GET /user profile.

@mtreacy002 mtreacy002 requested review from meenakshi-dhanani and a team July 2, 2020 13:49
fetch("http://127.0.0.1:5000/login", requestLogin)
.then(async response => {
let data = await response.json();
if (response.status === 200) {
if (data !== {}) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is your purpose to see if data is an empty object?
Please look into considerations for such tests, and I would recommend using one of the suggestions from here: https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @ramitsawhney27 . I've changed it to one of the suggested option (using underscore library _.isEmpty(obj)) 👍

src/login/Login.jsx Show resolved Hide resolved
@@ -25,21 +25,26 @@ export default function Login() {
},
body: JSON.stringify(payload)
};

fetch("http://127.0.0.1:5000/login", requestLogin)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a constant, like LOCALHOST="http://...."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Changed it to BASE_API_URL

@mtreacy002
Copy link
Member Author

Update @anitab-org/bridgeintech-maintainers and @ramitsawhney27 . I have made the changes as per @ramitsawhney27 suggestion. Please let me know if you have any other feedback 😉.

Copy link

@ramitsawhney27 ramitsawhney27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good to me, great work on the quick turnaround @mtreacy002!

@mtreacy002
Copy link
Member Author

mtreacy002 commented Jul 3, 2020

Just wanna share latest codesandbox incase anyone wanna try it out.
https://codesandbox.io/s/persistence-js-cookies-u0007?file=/src/Routes.js

Note: for some reason the app doesn't work the same on sandbox and VSCode. not sure why. To test the final code change, try to test directly from PR branch.

@mtreacy002
Copy link
Member Author

Update @meenakshi-dhanani and @anitab-org/bridgeintech-maintainers . I've fixed the bugs. Now this PR is complete. Logout option on Navbar now automatically shown after successful login.

src/Navigation.js Outdated Show resolved Hide resolved
src/Routes.js Outdated
);
export const SessionUser = createContext(Cookies.get("user"));
export const BASE_API_URL = "http://127.0.0.1:5000";

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could abstract all this Context to a separate component

I finally found a good example:
https://codesandbox.io/s/p71pr7jn50?file=/src/AuthContext.js

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can explain more on this in our catch up today.

@meenakshi-dhanani
Copy link
Contributor

Maya this is some great work! Especially because I know you had to research about everything and I haven't been of much help lately. Great work!
There is a slightly cleaner way of abstracting out the AuthContext, I found this just today, apologies for sharing it so late - https://codesandbox.io/s/p71pr7jn50?

We can pair and work on the changes together during our catchup.

@mtreacy002
Copy link
Member Author

Update @meenakshi-dhanani . I've merged your changes into this PR branch. Thanks for the fix. It looks great 😉.

@mtreacy002 mtreacy002 changed the title WIP: Feat: Issue#30 protected page and Persistent Token Feat: Issue#30 protected page and Persistent Token Jul 8, 2020
@meenakshi-dhanani
Copy link
Contributor

There is an unnecessary component for Logout, it could be done within a method and history.push instead of Redirect. It's fine though if we tackle that improvement in a separate PR. Since I understand this PR has been open for long.

Copy link
Contributor

@meenakshi-dhanani meenakshi-dhanani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested the following scenarios:

  • User has to login to access MySpace page
  • Once user logs in they can access MySpace
  • On logout, MySpace again redirects to Login
  • Other public pages can be viewed without Login

Also performed code review for the PR.

Auth-Protected-Page

@meenakshi-dhanani
Copy link
Contributor

@anitab-org/bridgeintech-maintainers can you review this PR?

@meenakshi-dhanani
Copy link
Contributor

@foongminwong can you review this PR?

@foongminwong
Copy link

@foongminwong can you review this PR?

Yes I'll review this by today

Copy link

@foongminwong foongminwong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes made in this PR were tested locally. Following are the results:

  1. Code review - Done by @meenakshi-dhanani and @ramitsawhney27

  2. All possible responses were tested as below:

Protected page & Persistent Token UI Expected Result Actual Result
test-pr-bit-web-34-before -myspace-login As a user, when I click on My Space, it prompts me to login.
test-pr-bit-web-34-after-myspace-login As a user, after I login into My Space, I should see a BIT private page for member's portfolio under My Space. A welcome message should show up and indicate that the user logs in successfully.
test-pr-bit-web-34-myspace-logout As a user, when I logout from My Space, it should direct me to a BIT public landing page.
  1. Additional testcases covered: N/A

  2. OS Version: Windows 10

@foongminwong foongminwong merged commit a3ee4e9 into anitab-org:develop Jul 9, 2020
@mtreacy002 mtreacy002 deleted the issue#30-protected-page-v2 branch September 12, 2020 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Coding Changes to code base or refactored code that doesn't fix a bug. Category: User Interface Improvements or additions to design. Program: GSOC Related to work completed during the Google Summer of Code Program.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Persist auth token on client side for each request to backend
4 participants