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

[WIP] Integrate login/signup and logout flow #44

Merged
merged 9 commits into from
May 14, 2020
6 changes: 4 additions & 2 deletions src/components/common/userMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class UserMenu extends Component {
<div className='usermenu--activitylog link'>Activity Log</div>
</Link>
<div className='usermenu--profile'><a href='http://accounts.sdslabs.co' className='link'>Profile</a></div>
<div className='usermenu--logout'>Logout</div>
<div className='usermenu--logout' onClick={() => this.props.loginHandler('logout')}>Logout</div>
</div>
</div> :
<Fragment/> }
Expand All @@ -49,5 +49,7 @@ UserMenu.propTypes = {
/** Function to toggle state of modals */
handleClick: PropTypes.func,
/** Function to close modals. */
close: PropTypes.func
close: PropTypes.func,
/** Function to login/register/logout */
loginHandler: PropTypes.func
};
10 changes: 6 additions & 4 deletions src/components/coursecard/coursePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@ class CoursePage extends Component {
async checkCourse(props) {
if(props.user.login) {
this.setState({ mycourse:false });
await props.user.courses.forEach(course => {
if(course.code === props.course_code)
this.setState({ mycourse:true });
});
if(props.user.courses) {
await props.user.courses.forEach(course => {
if(course.code === props.course_code)
this.setState({ mycourse:true });
});
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/components/header/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ class Header extends Component {
</div>
<div className='header--user'>
<UserMenu userMenu={this.props.userMenu}
loginHandler={this.props.loginHandler}
handleClick={this.props.handleClick}
close={this.props.close}/>
</div>
</Fragment>) :
(<Fragment>
<button className='header--login'>Login</button>
<button className='header--signup'>Sign Up</button>
<button className='header--login' onClick={() => this.props.loginHandler('login')}>Login</button>
<button className='header--signup' onClick={() => this.props.loginHandler('register')}>Sign Up</button>
</Fragment>)
}
</div>
Expand All @@ -88,5 +89,7 @@ Header.propTypes = {
/** Function to toggle see-all modal. */
handleSeeAllClick: PropTypes.func,
/** Holds user data which is handled through Redux. */
user: PropTypes.object
user: PropTypes.object,
/** Function to login/register/logout */
loginHandler: PropTypes.func
};
9 changes: 6 additions & 3 deletions src/components/home/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ class Header extends Component {
</div>
<div className='landingheader--user'>
<UserMenu userMenu={this.props.userMenu}
loginHandler={this.props.loginHandler}
handleClick={this.props.handleClick}
close={this.props.close}/>
</div>
</div> :
<div className='landingheader--user_nologin'>
<button className='landingheader--login'>Login</button>
<button className='landingheader--signup'>Sign Up</button>
<button className='landingheader--login' onClick={() => this.props.loginHandler('login')}>Login</button>
<button className='landingheader--signup' onClick={() => this.props.loginHandler('register')}>Sign Up</button>
</div> }
</div>
);
Expand All @@ -70,5 +71,7 @@ Header.propTypes = {
/** Function to toggle see-all modal. */
handleSeeAllClick: PropTypes.func,
/** Holds user data which is handled through Redux. */
user: PropTypes.object
user: PropTypes.object,
/** Function to login/register/logout */
loginHandler: PropTypes.func
};
7 changes: 5 additions & 2 deletions src/components/sidebar/courseHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ class CourseHandle extends Component {
* Check if course is registered for user.
*/
checkMyCourse(props) {
if(props.user.courses.find(o => o.code === props.code) !== undefined)
return true;
if(props.user.courses) {
if(props.user.courses.find(o => o.code === props.code) !== undefined)
return true;
else return false;
}
else return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/sidebar/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Sidebar extends Component {
</div>
<div className='sidebar--course-name'>
<div className='sidebar--course-table'>
{ this.props.user.courses.map((course) => (
{ this.props.user.courses ? this.props.user.courses.map((course) => (
<Link to={ `/mycourse/departments/${course.department.abbreviation}/courses/${course.code}/` } key={ course.id }>
<CourseHandle login
name={ `${course.title} ${course.code}` }
Expand All @@ -128,7 +128,7 @@ class Sidebar extends Component {
active={this.active}
handleClick={this.handleClick}/>
</Link>
)) }
)) : null}
</div>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion src/config/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const CONFIG = {
nexusRoot: "http://localhost:8005/api/v1"
nexusRoot: "http://localhost:8005/api/v1",
arceusRoot: "http://arceus.sdslabs.local",
Copy link
Member

Choose a reason for hiding this comment

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

why do we need this in axion?

studyRoot: "http://studyportal.sdslabs.local"
};
166 changes: 96 additions & 70 deletions src/pages/department.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { setUser, resetApp } from 'actions/actions';
import { loginUserWithToken, loginUserWithCookie } from 'api/userApi';
import { getCookie, removeCookie } from 'utils/handleCookies';
import ShowMoreFiles from 'components/header/showMoreFiles';
import { CONFIG } from 'config/config';

function mapStateToProps(state) {
return { user: state };
Expand Down Expand Up @@ -70,6 +71,7 @@ class Department extends Component {
this.handleSeeAllClick = this.handleSeeAllClick.bind(this);
this.close = this.close.bind(this);
this.error = this.error.bind(this);
this.loginHandler = this.loginHandler.bind(this);
}

// eslint-disable-next-line react/no-deprecated
Expand Down Expand Up @@ -190,80 +192,100 @@ class Department extends Component {
* Fetch user details.
*/
getUserDetails() {
const token = getCookie('token');
const cookie = getCookie('sdslabs');
if (token) {
loginUserWithToken(token).then((res) => {
const user = {
login: true,
id: res.user.falcon_id,
username: res.user.username,
email: res.user.email,
profile_image: res.user.profile_image,
courses: res.courses
};
if(!_.isEqual(user, this.props.user)) {
this.props.setUser(user);
// Logged in with token
}
})
.catch(() => {
// Token is corrupted
if (cookie) {
loginUserWithCookie().then((res) => {
const user = {
login: true,
id: res.user.falcon_id,
username: res.user.username,
email: res.user.email,
profile_image: res.user.profile_image,
courses: res.courses
};
if(!_.isEqual(user, this.props.user)) {
this.props.setUser(user);
// Logged in with cookie and the invalid token replaced
}
})
.catch(() => {
this.props.resetApp();
removeCookie('sdslabs');
removeCookie('token');
// The cookie is corrupted, both the token and the cookie have been removed
});
const token = getCookie('token');
const cookie = getCookie('sdslabs');
if (token) {
loginUserWithToken(token).then((res) => {
const user = {
login: true,
id: res.user.falcon_id,
username: res.user.username,
email: res.user.email,
profile_image: res.user.profile_image,
courses: res.courses
};
if(!_.isEqual(user, this.props.user)) {
this.props.setUser(user);
// Logged in with token
}
else {
this.props.resetApp();
// No cookie present and the token is corrupted
removeCookie('token');
})
.catch(() => {
// Token is corrupted
if (cookie) {
loginUserWithCookie().then((res) => {
const user = {
login: true,
id: res.user.falcon_id,
username: res.user.username,
email: res.user.email,
profile_image: res.user.profile_image,
courses: res.courses
};
if(!_.isEqual(user, this.props.user)) {
this.props.setUser(user);
// Logged in with cookie and the invalid token replaced
}
})
.catch(() => {
this.props.resetApp();
removeCookie('sdslabs');
removeCookie('token');
// The cookie is corrupted, both the token and the cookie have been removed
});
}
else {
this.props.resetApp();
// No cookie present and the token is corrupted
removeCookie('token');
}
});
}
else if (cookie) {
loginUserWithCookie().then((res) => {
const user = {
login: true,
id: res.user.falcon_id,
username: res.user.username,
email: res.user.email,
profile_image: res.user.profile_image,
courses: res.courses
};
if(!_.isEqual(user, this.props.user)) {
this.props.setUser(user);
// The user did not have the token but is logged in by the cookie and now the token has been created
}
});
}
else if (cookie) {
loginUserWithCookie().then((res) => {
const user = {
login: true,
id: res.user.falcon_id,
username: res.user.username,
email: res.user.email,
profile_image: res.user.profile_image,
courses: res.courses
};
if(!_.isEqual(user, this.props.user)) {
this.props.setUser(user);
// The user did not have the token but is logged in by the cookie and now the token has been created
}
})
.catch(() => {
this.props.resetApp();
removeCookie('sdslabs');
// The cookie is corrupted and removed
});
})
.catch(() => {
this.props.resetApp();
removeCookie('sdslabs');
// The cookie is corrupted and removed
});
}
else {
this.props.resetApp();
// Neither cookie nor token present
}
}
else {
this.props.resetApp();
// Neither cookie nor token present

/**
* Login/Register/Logout user.
*
* @param {string} value
*/
loginHandler(value) {
if (value === 'login') {
window.location.href = `${CONFIG.arceusRoot}/${value}?redirect=${window.location.href}`;
}
else if (value === 'register') {
window.location.href = `${CONFIG.arceusRoot}/${value}?redirect=${window.location.href}`;
}
else if (value === 'logout') {
this.props.resetApp();
window.location.href = CONFIG.studyRoot;
removeCookie('token');
removeCookie('sdslabs');
}
}
}

/**
* Toggle state of different modals.
Expand Down Expand Up @@ -390,6 +412,7 @@ class Department extends Component {
userMenu={this.state.userMenu}
handleClick ={this.handleClick}
handleSeeAllClick={this.handleSeeAllClick}
loginHandler={this.loginHandler}
close={this.close}/>
<Sidebar activity='mycourse'
department={this.state.department.title}
Expand Down Expand Up @@ -427,6 +450,7 @@ class Department extends Component {
userMenu={this.state.userMenu}
handleClick ={this.handleClick}
handleSeeAllClick={this.handleSeeAllClick}
loginHandler={this.loginHandler}
close={this.close}/>
<Sidebar activity='activity'
active={this.props.match.params.type}
Expand All @@ -452,6 +476,7 @@ class Department extends Component {
userMenu={this.state.userMenu}
handleClick ={this.handleClick}
handleSeeAllClick={this.handleSeeAllClick}
loginHandler={this.loginHandler}
close={this.close}/>
<Sidebar login={false}
department={this.state.department.title}
Expand Down Expand Up @@ -488,6 +513,7 @@ class Department extends Component {
userMenu={this.state.userMenu}
handleClick ={this.handleClick}
handleSeeAllClick={this.handleSeeAllClick}
loginHandler={this.loginHandler}
close={this.close}/>
<Request request={this.state.request} close={this.close} refreshRequest={this.refreshRequest}/>
<Upload upload={this.state.upload} close={this.close}/>
Expand Down
Loading