├── .babelrc ├── .eslintrc ├── .gitignore ├── LICENSE ├── README.md ├── babel-plugin-macros.config.js ├── components ├── Button.tsx ├── FormWithLabel.tsx ├── Header.tsx ├── InputWithError.tsx └── Logo.tsx ├── next-env.d.ts ├── next.config.js ├── package.json ├── pages ├── _app.js ├── _document.js ├── index.tsx ├── login.tsx └── register.tsx ├── postcss.config.js ├── public ├── favicon.ico ├── frog1.png ├── frog2.png └── vercel.svg ├── styles └── styles.css ├── tailwind.config.js ├── tsconfig.json └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["next/babel"], 3 | "plugins": [ 4 | "babel-plugin-macros", 5 | [ 6 | "styled-components", 7 | { 8 | "ssr": true, 9 | "displayName": true, 10 | "preprocess": true 11 | } 12 | ] 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-twg/typescript.js", 3 | "parserOptions": { 4 | "project": "./tsconfig.json" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env.local 29 | .env.development.local 30 | .env.test.local 31 | .env.production.local 32 | 33 | # vercel 34 | .vercel 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 TWG 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repo is part of our [engineering blog](https://thewidlarzgroup.com/blog/) at TWG :fire: 2 | --- 3 | ### Installation: 4 | 5 | - [ ] Clone and run [backend repo](https://github.com/TheWidlarzGroup/JWTAuthBackend) 6 | - [ ] Run `yarn` from the root 7 | - [ ] Run the app with yarn start 8 | 9 | Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. 10 | 11 | You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file. 12 | 13 | -------------------------------------------------------------------------------- /babel-plugin-macros.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | twin: { 3 | config: 'tailwind.config.js', 4 | preset: 'styled-components', 5 | dataTwProp: true, 6 | debugPlugins: false, 7 | debug: false, 8 | }, 9 | } 10 | -------------------------------------------------------------------------------- /components/Button.tsx: -------------------------------------------------------------------------------- 1 | import styled from 'styled-components' 2 | import React from 'react' 3 | 4 | export const StyledButton = styled.button` 5 | &:hover { 6 | transform: scale(1.1); 7 | } 8 | &:active { 9 | transform: scale(1); 10 | } 11 | &:focus { 12 | box-shadow: 0px 0px 0px 3px rgba(0, 0, 0, 0.1); 13 | } 14 | ` 15 | 16 | export interface Props { 17 | className?: string 18 | onClick?: () => void 19 | } 20 | 21 | const Button: React.FC = ({ children, className, onClick }) => ( 22 | 26 | {children} 27 | 28 | ) 29 | 30 | export default Button 31 | -------------------------------------------------------------------------------- /components/FormWithLabel.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import styled from 'styled-components' 3 | import Button from './Button' 4 | 5 | export interface Props { 6 | onSubmit: () => void 7 | onRedirect: () => void 8 | topText: string 9 | redirectText: string 10 | buttonText: string 11 | } 12 | 13 | export const StyledButton = styled.button` 14 | &:hover { 15 | transform: scale(1.1); 16 | } 17 | &:focus { 18 | border: 1px solid #236645; 19 | } 20 | ` 21 | 22 | const FormWithLabel: React.FC = ({ 23 | children, 24 | onSubmit, 25 | topText, 26 | buttonText, 27 | onRedirect, 28 | redirectText, 29 | }) => ( 30 |
33 |

{topText}

34 | {children} 35 | 36 |
37 | {redirectText} 38 |
39 |
40 | ) 41 | 42 | export default FormWithLabel 43 | -------------------------------------------------------------------------------- /components/Header.tsx: -------------------------------------------------------------------------------- 1 | import { useRouter } from 'next/dist/client/router' 2 | import React from 'react' 3 | 4 | import Button from './Button' 5 | 6 | export const Header: React.FC = ({ children }) => { 7 | const router = useRouter() 8 | return ( 9 |
10 | 17 | {children} 18 |
19 | ) 20 | } 21 | -------------------------------------------------------------------------------- /components/InputWithError.tsx: -------------------------------------------------------------------------------- 1 | import { FormikHandlers, FormikProps, FormikValues } from 'formik' 2 | import React from 'react' 3 | import styled from 'styled-components' 4 | 5 | export interface Props { 6 | formik: FormikProps 7 | name: string 8 | onChange?: FormikHandlers['handleChange'] 9 | label?: string 10 | type?: string 11 | } 12 | 13 | export const StyledInput = styled.input.attrs({ 14 | className: `transition-colors shadow-xl-light duration-500 border-solid border-transparent w-full bg-ice-blue text-14 mb-1 font-medium rounded-xl px-5 h-12 mb-0 text-slate-gray focus:outline-none focus:border-primary placeholder-light-gray-blue 15 | `, 16 | })` 17 | border-width: 1px; 18 | ` 19 | 20 | const InputWithError: React.FC = ({ 21 | label, 22 | formik: { values, errors, touched, handleChange }, 23 | name, 24 | onChange = handleChange, 25 | ...rest 26 | }) => ( 27 |
28 | 31 | 32 |

{touched[name] && errors[name]}

33 |
34 | ) 35 | 36 | export default InputWithError 37 | -------------------------------------------------------------------------------- /components/Logo.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import Image from 'next/image' 3 | 4 | const Logo = () => ( 5 |
6 | me 7 |

Frog Auth

8 |
9 | ) 10 | 11 | export default Logo 12 | -------------------------------------------------------------------------------- /next-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | images: { 3 | domains: ['pixabay.com'], 4 | }, 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "next-jwt-auth", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "format": "prettier --write \"**/*.{js,jsx,ts,tsx}\"", 10 | "lint": "eslint ./ --ext .js,.ts,.tsx" 11 | }, 12 | "prettier": "@twgdev/prettier-config", 13 | "dependencies": { 14 | "@reduxjs/toolkit": "^1.5.0", 15 | "axios": "^0.21.1", 16 | "axios-auth-refresh": "^3.1.0", 17 | "cookie": "^0.4.1", 18 | "formik": "^2.2.6", 19 | "next": "10.0.5", 20 | "next-redux-wrapper": "^6.0.2", 21 | "react": "17.0.1", 22 | "react-dom": "17.0.1", 23 | "react-redux": "^7.2.2", 24 | "redux-persist": "^6.0.0", 25 | "set-cookie-parser": "^2.4.7", 26 | "styled-components": "latest", 27 | "styled-jsx": "^3.4.4", 28 | "twin.macro": "^2.1.1", 29 | "yup": "^0.32.8" 30 | }, 31 | "devDependencies": { 32 | "@twgdev/prettier-config": "^1.0.2", 33 | "@types/cookie": "^0.4.0", 34 | "@types/node": "^14.14.25", 35 | "@types/react-redux": "^7.1.16", 36 | "@types/set-cookie-parser": "^2.4.0", 37 | "@types/styled-components": "^5.1.7", 38 | "@typescript-eslint/eslint-plugin": "^4.15.0", 39 | "@typescript-eslint/parser": "^4.15.0", 40 | "autoprefixer": "^10.2.3", 41 | "babel-eslint": "^10.1.0", 42 | "babel-plugin-styled-components": "^1.12.0", 43 | "eslint": "^7.19.0", 44 | "eslint-config-airbnb": "^18.2.1", 45 | "eslint-config-airbnb-typescript": "^12.3.1", 46 | "eslint-config-prettier": "^7.2.0", 47 | "eslint-config-twg": "1.0.2", 48 | "eslint-plugin-html": "^6.1.1", 49 | "eslint-plugin-import": "^2.22.1", 50 | "eslint-plugin-jsx-a11y": "^6.4.1", 51 | "eslint-plugin-prettier": "^3.3.1", 52 | "eslint-plugin-react": "^7.22.0", 53 | "eslint-plugin-react-hooks": "^4.2.0", 54 | "postcss": "^8.2.4", 55 | "prettier": "^2.2.1", 56 | "tailwindcss": "^2.0.2", 57 | "typescript": "^4.1.4" 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /pages/_app.js: -------------------------------------------------------------------------------- 1 | import '../styles/styles.css' 2 | import styled from 'styled-components' 3 | import tw from 'twin.macro' 4 | import { Header } from '../components/Header' 5 | 6 | export const PageWrapper = styled.main` 7 | ${tw`text-near-black bg-ice-blue w-full relative`} 8 | min-height: 100vh; 9 | ` 10 | 11 | function MyApp({ Component, pageProps }) { 12 | return ( 13 | 14 |
15 | 16 |
17 |
18 | ) 19 | } 20 | 21 | export default MyApp 22 | -------------------------------------------------------------------------------- /pages/_document.js: -------------------------------------------------------------------------------- 1 | import Document from 'next/document' 2 | import { ServerStyleSheet } from 'styled-components' 3 | import flush from 'styled-jsx/server' 4 | 5 | export default class MyDocument extends Document { 6 | static async getInitialProps(ctx) { 7 | const sheet = new ServerStyleSheet() 8 | const originalRenderPage = ctx.renderPage 9 | 10 | try { 11 | ctx.renderPage = () => 12 | originalRenderPage({ 13 | enhanceApp: (App) => (props) => sheet.collectStyles(), 14 | }) 15 | 16 | const initialProps = await Document.getInitialProps(ctx) 17 | return { 18 | ...initialProps, 19 | styles: ( 20 | <> 21 | {initialProps.styles} 22 | {sheet.getStyleElement()} 23 | {flush() || null} 24 | 25 | ), 26 | } 27 | } finally { 28 | sheet.seal() 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /pages/index.tsx: -------------------------------------------------------------------------------- 1 | import { useRouter } from 'next/dist/client/router' 2 | import axios from 'axios' 3 | import Image from 'next/image' 4 | import React from 'react' 5 | import Button from '../components/Button' 6 | 7 | type Frog = { id: string; webformatURL: string } 8 | 9 | export const Home = ({ frogs }: { frogs: Frog[] }) => { 10 | const router = useRouter() 11 | return ( 12 |
13 |
14 | 21 |

22 | {frogs ? ( 23 | 'You may pet the phrog 🐸' 24 | ) : ( 25 | <> 26 | router.push('/login')}> 29 | Login 30 | {' '} 31 | to pet the phrog 👀 32 | 33 | )} 34 |

35 | {frogs?.map((frog, index) => ( 36 |
37 | 38 |
39 | ))} 40 |
41 |
42 | ) 43 | } 44 | 45 | export const getServerSideProps = async () => { 46 | const response = await axios.get( 47 | 'https://pixabay.com/api/?key=20330556-9d467084be89e92c1e9632c3a&q=frog&image_type=photo' 48 | ) 49 | 50 | return { props: { frogs: response.data.hits } } 51 | } 52 | 53 | export default Home 54 | -------------------------------------------------------------------------------- /pages/login.tsx: -------------------------------------------------------------------------------- 1 | import { useFormik } from 'formik' 2 | import React from 'react' 3 | import * as yup from 'yup' 4 | import { useRouter } from 'next/dist/client/router' 5 | import styled from 'styled-components' 6 | import tw from 'twin.macro' 7 | import InputWithError from '../components/InputWithError' 8 | import FormWithLabel from '../components/FormWithLabel' 9 | import Logo from '../components/Logo' 10 | import Button from '../components/Button' 11 | 12 | interface Values { 13 | email: string 14 | password: string 15 | } 16 | 17 | const loginSchema = yup.object({ 18 | email: yup.string().email('Provide correct e-mail').required('Required'), 19 | password: yup.string().required('Required'), 20 | }) 21 | 22 | const initialValues: Values = { 23 | email: '', 24 | password: '', 25 | } 26 | export const PageWrapper = styled.div` 27 | ${tw`text-near-black bg-ice-blue w-full flex flex-col items-center justify-center`} 28 | height: 100vh; 29 | ` 30 | 31 | const Auth = () => { 32 | const router = useRouter() 33 | 34 | const formik = useFormik({ 35 | validationSchema: loginSchema, 36 | initialValues, 37 | onSubmit: () => { 38 | router.push('/') 39 | }, 40 | }) 41 | 42 | return ( 43 | 44 | 51 | 52 | router.push('/register')}> 58 | 59 | 60 | 61 | 62 | ) 63 | } 64 | 65 | export default Auth 66 | -------------------------------------------------------------------------------- /pages/register.tsx: -------------------------------------------------------------------------------- 1 | import { useFormik } from 'formik' 2 | import React from 'react' 3 | import * as yup from 'yup' 4 | import { useRouter } from 'next/dist/client/router' 5 | import { PageWrapper } from './login' 6 | import Logo from '../components/Logo' 7 | import FormWithLabel from '../components/FormWithLabel' 8 | import InputWithError from '../components/InputWithError' 9 | 10 | interface Values { 11 | email: string 12 | password: string 13 | name: string 14 | } 15 | 16 | const loginSchema = yup.object({ 17 | email: yup.string().email().required(), 18 | password: yup.string().required(), 19 | name: yup.string().required(), 20 | }) 21 | 22 | const initialValues: Values = { 23 | email: '', 24 | password: '', 25 | name: '', 26 | } 27 | 28 | const Auth = () => { 29 | const router = useRouter() 30 | 31 | const formik = useFormik({ 32 | validationSchema: loginSchema, 33 | initialValues, 34 | onSubmit: () => { 35 | router.push('/') 36 | }, 37 | }) 38 | 39 | return ( 40 | 41 | 42 | router.push('/login')}> 48 | 49 | 50 | 51 | 52 | 53 | ) 54 | } 55 | 56 | export default Auth 57 | -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheWidlarzGroup/nextjs-authentication/6951ec4ed618606df3f89ad75c252a837f92d156/public/favicon.ico -------------------------------------------------------------------------------- /public/frog1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheWidlarzGroup/nextjs-authentication/6951ec4ed618606df3f89ad75c252a837f92d156/public/frog1.png -------------------------------------------------------------------------------- /public/frog2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheWidlarzGroup/nextjs-authentication/6951ec4ed618606df3f89ad75c252a837f92d156/public/frog2.png -------------------------------------------------------------------------------- /public/vercel.svg: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /styles/styles.css: -------------------------------------------------------------------------------- 1 | @import 'tailwindcss/base'; 2 | @import 'tailwindcss/components'; 3 | @import 'tailwindcss/utilities'; 4 | @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@100;200;300;400;500;600;700&display=swap'); 5 | 6 | html { 7 | box-sizing: border-box; 8 | } 9 | *, 10 | *:before, 11 | *:after { 12 | box-sizing: inherit; 13 | } 14 | 15 | body { 16 | margin: 0; 17 | font-family: 'Montserrat', sans-serif; 18 | -webkit-font-smoothing: antialiased; 19 | -moz-osx-font-smoothing: grayscale; 20 | } 21 | 22 | a { 23 | color: inherit; 24 | } 25 | 26 | code { 27 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace; 28 | } 29 | -------------------------------------------------------------------------------- /tailwind.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | purge: ['./components/**/*.{js,ts,jsx,tsx}', './pages/**/*.{js,ts,jsx,tsx}'], 3 | darkMode: false, // or 'media' or 'class' 4 | theme: { 5 | fontFamily: { 6 | sans: ['sofiapro'], 7 | }, 8 | fontWeight: { 9 | light: 300, 10 | normal: 400, 11 | medium: 500, 12 | semibold: 600, 13 | bold: 700, 14 | }, 15 | extend: { 16 | fontSize: { 17 | 12: '12px', 18 | 14: '14px', 19 | 16: '16px', 20 | 18: '18px', 21 | 72: '72px', 22 | }, 23 | colors: { 24 | 'near-black': '#1d2129', 25 | 'ice-blue': '#f0f4f7', 26 | 'light-orange': '#fff7e8', 27 | primary: '#4EE69C', 28 | 'light-black': '#565a64', 29 | }, 30 | borderRadius: { 31 | l: '8px', 32 | xl: '16px', 33 | xxl: '24px', 34 | xxxl: '36px', 35 | xxxl2: '42px', 36 | }, 37 | boxShadow: { 38 | 'xl-light': '0 5px 20px 0 rgba(86, 90, 100, 0.05);', 39 | 'sm-light': '0 2px 3px 0 rgba(86, 90, 100, 0.1);', 40 | }, 41 | letterSpacing: { 42 | '1px': '1px', 43 | }, 44 | }, 45 | }, 46 | variants: { 47 | extend: {}, 48 | }, 49 | plugins: [], 50 | } 51 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-twg/tsconfig.json", 3 | "compilerOptions": { 4 | "target": "es5", 5 | "lib": [ 6 | "dom", 7 | "dom.iterable", 8 | "esnext" 9 | ], 10 | "allowJs": true, 11 | "skipLibCheck": true, 12 | "strict": false, 13 | "forceConsistentCasingInFileNames": true, 14 | "noEmit": true, 15 | "esModuleInterop": true, 16 | "module": "esnext", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "jsx": "preserve" 21 | }, 22 | "include": [ 23 | "next-env.d.ts", 24 | "**/*", 25 | "**/*.ts", 26 | "**/*.tsx" 27 | ], 28 | "exclude": [ 29 | "node_modules" 30 | ] 31 | } 32 | --------------------------------------------------------------------------------