项目作者: mohammadou1

项目描述 :
A simple NextJS boilerplate, multi languages with SSG support,client side authentication hooks tailwindcss yet to have bootstrap and other libraries support
高级语言: TypeScript
项目地址: git://github.com/mohammadou1/nextcrazy.git
创建时间: 2020-08-29T10:04:44Z
项目社区:https://github.com/mohammadou1/nextcrazy

开源协议:

下载


NextCrazy Boilerplate

NextCrazy is really crazy

A boilerplate that gathers few things you may find helpful in any way,
please feel free to suggest changes, fixes or anything you find suitable

So far it only supports tailwind, but supporting bootstrap is coming!

There is a nice expermintal cli for this boiler as well :)

  1. npm i -g nextcrazy-cli

read more about it at NextCrazy Cli or just type nextcrazy and check it!

Features

  • NextJS 9.5
  • Multilanguage support (SSG supported)
  • Tailwind, and PostCSS
  • Authentication (Expermintal)
  • Google Tag Manager
  • Enviroment staging, (production,testing and development)
  • Typescript, Eslint and Prettier
  • React hook form example

Multilanguage

  • translate function + component
  • RTL & LTR support

Some parts were inspired by next-translate, thanks to them this was possible

Authentication

This part is tricky, and I don’t think it’s complete yet, but feel free to use it or improve it.

comes with few props to help you have better control over the authentication flow

all of the links should match NextJS link shape (href & as), though they announced that they will support using href only soon

Prop Description
loginPath Login page path (default is /[lang]/login)
afterLoginTo Where is the user going to redirect to after login?
afterLogoutTo Where is the user going to redirect to after logout?
onAuthTo Pages like login page can be wrapped with “withRedirectOnAuth” HOC, which will prevent the logged in user from visiting them, this prop will redirect them to the specified url (default is /[lang]/index)

And you can use “useAuth” hook, useAuth got plenty of useful thinks to access

login, it will only store the token in cookies and update login state, optionally it can store the user in the provider but not in cookies

  1. import useAuth from '~/auth/useAuth';
  2. const { login, logout } = useAuth();
  3. const loginHandler = () => {
  4. const user = { name: 'MY NAME' };
  5. // second paramater is remember_me which will extend the expiry of the token based on the stored value in .env files
  6. login('token goes here', true, user);
  7. };
  8. const logoutHandler = () => {
  9. // call some api to logout for example
  10. logout();
  11. };

You can access user related data as well!

  1. import useAuth from '~/auth/useAuth';
  2. const { token, user, authenticated, updateUser } = useAuth();
  3. // since the user is not stored in cookies, the data will be lost, its good to update the user data if user is not there
  4. useEffect(() => {
  5. if (!user && authenticated) {
  6. fetchSomeApi(token).then(data => updateUser(data));
  7. }
  8. }, [token, updateUser]);
  9. return authenticated ? <div>{user && <div>{JSON.stringify(user)}</div>}</div> : 'Not authenticated';

Other than login and logout, there is a tricky one called “comebackLogin”,
useful if clicks on an action that requires a logged in state (checkout for example)
if he successfully logs in, he will be redirected back to the desired location (default is the current one)

  1. import useAuth from '~/auth/useAuth';
  2. const { comebackLogin, authenticated } = useAuth();
  3. const checkoutHandler = () => {
  4. if (!authenticated) comebackLogin();
  5. else {
  6. // do something
  7. }
  8. };
  9. return <button onClick={checkoutHandler}>checkout</button>;

also we can redirect the user to another page upon login with the same action

  1. import useAuth from '~/auth/useAuth';
  2. import useTranslate from '~/i18n/useTranslate';
  3. const { comebackLogin, authenticated } = useAuth();
  4. const {lang} = useTranslate();
  5. const checkoutHandler = () => {
  6. // redirectTo will override going to login page and redirect to the desired path.
  7. if (!authenticated) comebackLogin({
  8. comebackTo:{
  9. href:'/[lang]/checkout'
  10. as:`/${lang}/checkout`
  11. }
  12. });
  13. else {
  14. // do something
  15. }
  16. };
  17. return <button onClick={checkoutHandler}>checkout</button>;

Additionally we can use withAuth HOC to protect a router (client side protection, SSR protection isn’t added yet)
The user will be redirected to login page

  1. import { withAuth } from '~/auth/useAuth';
  2. const ProfilePage = () => {
  3. return <div>profile page</div>;
  4. };
  5. export default withAuth(ProfilePage);

And you can pass an optional object with “comeback:true” to redirect back the user to this specific page after login,
it will override the provider default paths for this specific request

  1. import { withAuth } from '~/auth/useAuth';
  2. const ProfilePage = () => {
  3. return <div>profile page</div>;
  4. };
  5. export default withAuth(ProfilePage, { comeback: true });