Вы можете зарегистрироваться в getInitialProps
с некоторым ок.Логика оценивает куки, чтобы решить, следует ли перенаправить или нет.
import Router from 'next/router'
const redirectToLogin = res => {
if (res) {
res.writeHead(302, {Location: '/login'})
res.end()
res.finished = true
} else {
Router.push('/login')
}
}
class ProtectedPage extends React.Component {
static async getInitialProps ({req, res}) {
// get cookie from req.headers or from document.cookie in browser
// this cookie must not contain sensitive information!
const profile = getProfileFromCookie(req)
if (!profile) {
redirectToLogin(res)
}
}
}
Посмотрите на этот пример кода https://github.com/lipp/login-with/blob/master/example/nextjs/with-profile.js#L8 (я автор).