Created
March 6, 2020 14:43
-
-
Save kayode-adechinan/af2324c4c09e553b3cf96f6268ddd5cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// login.jsx | |
import React, { Component } from "react"; | |
import cookie from 'js-cookie' | |
import Router from 'next/router' | |
export default class Login extends Component { | |
login = () => { | |
cookie.set('status', "logged", { expires: 1 }) | |
Router.push('/profile') | |
}; | |
render() { | |
return ( | |
<div> | |
Click to log in | |
<button onClick={this.login}>Login</button> | |
</div> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import cookie from 'js-cookie' | |
import Router from 'next/router' | |
import nextCookie from 'next-cookies' | |
export default class Profile extends Component { | |
static async getInitialProps(ctx) { | |
const { status } = nextCookie(ctx) | |
console.log(status) | |
if (ctx.req && !status) { | |
ctx.res.writeHead(302, { Location: '/login' }) | |
ctx.res.end() | |
return | |
} | |
return {status} | |
} | |
logout = () => { | |
cookie.remove("status"); | |
Router.push('/login') | |
}; | |
render() { | |
return ( | |
<div> | |
Welcome deny | |
<button onClick={this.logout}>Logout</button> | |
</div> | |
); | |
} | |
} |
Author
kayode-adechinan
commented
Mar 6, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment