forked from acouzens/open5gs
fix the bug for session manangement
This commit is contained in:
parent
d25b45bf1c
commit
65bdbb037b
|
@ -1,12 +1,12 @@
|
|||
import React from 'react'
|
||||
import Session from './session'
|
||||
import React from 'react';
|
||||
import Session from './session';
|
||||
|
||||
export default class extends React.Component {
|
||||
|
||||
// Expose session to all pages
|
||||
static async getInitialProps({req}) {
|
||||
const session = new Session({req})
|
||||
return {session: await session.getSession()}
|
||||
static async getInitialProps({ req }) {
|
||||
const session = new Session({ req })
|
||||
return { session: await session.getSession() }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import Link from 'next/prefetch'
|
||||
import withSession from './with-session'
|
||||
import Link from 'next/link';
|
||||
import withSession from './with-session';
|
||||
|
||||
const Unauthorized = () => (
|
||||
<div>
|
||||
<h1>Unauthorized</h1>
|
||||
<p>You are not authorized to view this page.</p>
|
||||
<p><Link href='/login'><a>Please log in</a></Link></p>
|
||||
<p><Link href='/'><a>Back to homepage</a></Link></p>
|
||||
<p><Link prefetch href='/login'><a>Please log in</a></Link></p>
|
||||
<p><Link prefetch href='/'><a>Back to homepage</a></Link></p>
|
||||
</div>
|
||||
)
|
||||
|
||||
|
@ -15,5 +15,5 @@ export default (Component) => {
|
|||
return props.isLoggedIn ? <Component {...props} /> : <Unauthorized />
|
||||
}
|
||||
|
||||
return withSession(checkAuth)
|
||||
return withSession(checkAuth);
|
||||
}
|
|
@ -140,6 +140,9 @@ export default class Session {
|
|||
return reject(Error('XMLHttpRequest error: Error while attempting to signin'))
|
||||
}
|
||||
|
||||
// Update local session data
|
||||
this._session = await this.getSession(true)
|
||||
|
||||
return resolve(true)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,23 +1,22 @@
|
|||
import React from 'react'
|
||||
import Session from './session'
|
||||
import React from 'react';
|
||||
import Session from './session';
|
||||
|
||||
export default (Component) => class extends React.Component {
|
||||
static async getInitialProps (ctx) {
|
||||
const session = new Session({ req: ctx.req })
|
||||
const session = new Session({ req: ctx.req });
|
||||
|
||||
let initialProps = {}
|
||||
if (Component.getInitialProps) {
|
||||
initialProps = Component.getInitialProps({ ...ctx, session })
|
||||
initialProps = Component.getInitialProps({ ...ctx, session });
|
||||
}
|
||||
|
||||
const sessionData = await session.getSession()
|
||||
console.log(sessionData)
|
||||
let isLoggedIn = false
|
||||
if (sessionData.user && sessionData.user.id) {
|
||||
isLoggedIn = true
|
||||
const sessionData = await session.getSession();
|
||||
let isLoggedIn = false;
|
||||
if (sessionData.user && sessionData.user.username) {
|
||||
isLoggedIn = true;
|
||||
}
|
||||
|
||||
return {session: sessionData, isLoggedIn, ...initialProps}
|
||||
return { session: sessionData, isLoggedIn, ...initialProps }
|
||||
}
|
||||
|
||||
render () {
|
||||
|
|
|
@ -5,15 +5,9 @@ import Link from 'next/link'
|
|||
import React from 'react'
|
||||
import Page from '../components/page'
|
||||
import Layout from '../components/layout'
|
||||
import Session from '../components/session'
|
||||
|
||||
export default class extends Page {
|
||||
|
||||
static async getInitialProps({req}) {
|
||||
const session = new Session({req})
|
||||
return {session: await session.getSession(true)}
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Layout session={this.props.session}>
|
||||
|
|
|
@ -60,7 +60,7 @@ export default class extends Page {
|
|||
const session = new Session()
|
||||
session.signin(this.state.username, this.state.password)
|
||||
.then(() => {
|
||||
Router.push('/')
|
||||
this.props.url.push('/');
|
||||
})
|
||||
.catch(err => {
|
||||
// @FIXME Handle error
|
||||
|
|
Loading…
Reference in New Issue