├── static ├── favicon.ico └── README.md ├── middleware ├── auth.js ├── session-control.js └── README.md ├── .vscode └── settings.json ├── components ├── README.md ├── Logo.vue └── Header.vue ├── layouts ├── default.vue └── README.md ├── .editorconfig ├── pages ├── README.md ├── about.vue ├── auth │ └── index.vue └── index.vue ├── assets └── README.md ├── plugins └── README.md ├── package.json ├── README.md ├── store ├── README.md └── index.js ├── .gitignore └── nuxt.config.js /static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmilAbdullazadeh/auth-nuxt-project/HEAD/static/favicon.ico -------------------------------------------------------------------------------- /middleware/auth.js: -------------------------------------------------------------------------------- 1 | export default function(context) { 2 | if (!context.store.getters.isAuthenticated){ 3 | context.redirect('/auth'); 4 | } 5 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "vue3snippets.enable-compile-vue-file-on-did-save-code": false, 3 | "compile-hero.disable-compile-files-on-did-save-code": true 4 | } -------------------------------------------------------------------------------- /middleware/session-control.js: -------------------------------------------------------------------------------- 1 | export default function (context) { 2 | if(process.client){ 3 | context.store.dispatch("initAuth") 4 | } else { 5 | context.store.dispatch("initAuth", context.req) 6 | } 7 | } -------------------------------------------------------------------------------- /components/README.md: -------------------------------------------------------------------------------- 1 | # COMPONENTS 2 | 3 | **This directory is not required, you can delete it if you don't want to use it.** 4 | 5 | The components directory contains your Vue.js Components. 6 | 7 | _Nuxt.js doesn't supercharge these components._ 8 | -------------------------------------------------------------------------------- /layouts/default.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 14 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /layouts/README.md: -------------------------------------------------------------------------------- 1 | # LAYOUTS 2 | 3 | **This directory is not required, you can delete it if you don't want to use it.** 4 | 5 | This directory contains your Application Layouts. 6 | 7 | More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/views#layouts). 8 | -------------------------------------------------------------------------------- /pages/README.md: -------------------------------------------------------------------------------- 1 | # PAGES 2 | 3 | This directory contains your Application Views and Routes. 4 | The framework reads all the `*.vue` files inside this directory and creates the router of your application. 5 | 6 | More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/routing). 7 | -------------------------------------------------------------------------------- /assets/README.md: -------------------------------------------------------------------------------- 1 | # ASSETS 2 | 3 | **This directory is not required, you can delete it if you don't want to use it.** 4 | 5 | This directory contains your un-compiled assets such as LESS, SASS, or JavaScript. 6 | 7 | More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/assets#webpacked). 8 | -------------------------------------------------------------------------------- /plugins/README.md: -------------------------------------------------------------------------------- 1 | # PLUGINS 2 | 3 | **This directory is not required, you can delete it if you don't want to use it.** 4 | 5 | This directory contains Javascript plugins that you want to run before mounting the root Vue.js application. 6 | 7 | More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/plugins). 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "auth-nuxt-project", 3 | "version": "1.0.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "nuxt", 7 | "build": "nuxt build", 8 | "start": "nuxt start", 9 | "generate": "nuxt generate" 10 | }, 11 | "dependencies": { 12 | "axios": "^0.20.0", 13 | "js-cookie": "^2.2.1", 14 | "nuxt": "^2.14.0" 15 | }, 16 | "devDependencies": {} 17 | } 18 | -------------------------------------------------------------------------------- /middleware/README.md: -------------------------------------------------------------------------------- 1 | # MIDDLEWARE 2 | 3 | **This directory is not required, you can delete it if you don't want to use it.** 4 | 5 | This directory contains your application middleware. 6 | Middleware let you define custom functions that can be run before rendering either a page or a group of pages. 7 | 8 | More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/routing#middleware). 9 | -------------------------------------------------------------------------------- /pages/about.vue: -------------------------------------------------------------------------------- 1 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # auth-nuxt-project 2 | 3 | ## Build Setup 4 | 5 | ```bash 6 | # install dependencies 7 | $ npm install 8 | 9 | # serve with hot reload at localhost:3000 10 | $ npm run dev 11 | 12 | # build for production and launch server 13 | $ npm run build 14 | $ npm run start 15 | 16 | # generate static project 17 | $ npm run generate 18 | ``` 19 | 20 | For detailed explanation on how things work, check out [Nuxt.js docs](https://nuxtjs.org). 21 | -------------------------------------------------------------------------------- /store/README.md: -------------------------------------------------------------------------------- 1 | # STORE 2 | 3 | **This directory is not required, you can delete it if you don't want to use it.** 4 | 5 | This directory contains your Vuex Store files. 6 | Vuex Store option is implemented in the Nuxt.js framework. 7 | 8 | Creating a file in this directory automatically activates the option in the framework. 9 | 10 | More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/vuex-store). 11 | -------------------------------------------------------------------------------- /static/README.md: -------------------------------------------------------------------------------- 1 | # STATIC 2 | 3 | **This directory is not required, you can delete it if you don't want to use it.** 4 | 5 | This directory contains your static files. 6 | Each file inside this directory is mapped to `/`. 7 | Thus you'd want to delete this README.md before deploying to production. 8 | 9 | Example: `/static/robots.txt` is mapped as `/robots.txt`. 10 | 11 | More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/assets#static). 12 | -------------------------------------------------------------------------------- /components/Logo.vue: -------------------------------------------------------------------------------- 1 | 17 | 18 | 30 | -------------------------------------------------------------------------------- /components/Header.vue: -------------------------------------------------------------------------------- 1 | 21 | 22 | 40 | 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Node template 3 | # Logs 4 | /logs 5 | *.log 6 | npm-debug.log* 7 | yarn-debug.log* 8 | yarn-error.log* 9 | 10 | # Runtime data 11 | pids 12 | *.pid 13 | *.seed 14 | *.pid.lock 15 | 16 | # Directory for instrumented libs generated by jscoverage/JSCover 17 | lib-cov 18 | 19 | # Coverage directory used by tools like istanbul 20 | coverage 21 | 22 | # nyc test coverage 23 | .nyc_output 24 | 25 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 26 | .grunt 27 | 28 | # Bower dependency directory (https://bower.io/) 29 | bower_components 30 | 31 | # node-waf configuration 32 | .lock-wscript 33 | 34 | # Compiled binary addons (https://nodejs.org/api/addons.html) 35 | build/Release 36 | 37 | # Dependency directories 38 | node_modules/ 39 | jspm_packages/ 40 | 41 | # TypeScript v1 declaration files 42 | typings/ 43 | 44 | # Optional npm cache directory 45 | .npm 46 | 47 | # Optional eslint cache 48 | .eslintcache 49 | 50 | # Optional REPL history 51 | .node_repl_history 52 | 53 | # Output of 'npm pack' 54 | *.tgz 55 | 56 | # Yarn Integrity file 57 | .yarn-integrity 58 | 59 | # dotenv environment variables file 60 | .env 61 | 62 | # parcel-bundler cache (https://parceljs.org/) 63 | .cache 64 | 65 | # next.js build output 66 | .next 67 | 68 | # nuxt.js build output 69 | .nuxt 70 | 71 | # Nuxt generate 72 | dist 73 | 74 | # vuepress build output 75 | .vuepress/dist 76 | 77 | # Serverless directories 78 | .serverless 79 | 80 | # IDE / Editor 81 | .idea 82 | 83 | # Service worker 84 | sw.* 85 | 86 | # macOS 87 | .DS_Store 88 | 89 | # Vim swap files 90 | *.swp 91 | -------------------------------------------------------------------------------- /nuxt.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | /* 3 | ** Nuxt rendering mode 4 | ** See https://nuxtjs.org/api/configuration-mode 5 | */ 6 | mode: "universal", 7 | /* 8 | ** Nuxt target 9 | ** See https://nuxtjs.org/api/configuration-target 10 | */ 11 | target: "server", 12 | /* 13 | ** Headers of the page 14 | ** See https://nuxtjs.org/api/configuration-head 15 | */ 16 | head: { 17 | title: process.env.npm_package_name || "", 18 | meta: [ 19 | { charset: "utf-8" }, 20 | { name: "viewport", content: "width=device-width, initial-scale=1" }, 21 | { 22 | hid: "description", 23 | name: "description", 24 | content: process.env.npm_package_description || "" 25 | } 26 | ], 27 | link: [ 28 | { rel: "icon", type: "image/x-icon", href: "/favicon.ico" }, 29 | { 30 | rel: "stylesheet", 31 | href: 32 | "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css", 33 | integrity: 34 | "sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z", 35 | crossorigin: "anonymous" 36 | } 37 | ] 38 | }, 39 | /* 40 | ** Global CSS 41 | */ 42 | css: [], 43 | /* 44 | ** Plugins to load before mounting the App 45 | ** https://nuxtjs.org/guide/plugins 46 | */ 47 | plugins: [], 48 | /* 49 | ** Auto import components 50 | ** See https://nuxtjs.org/api/configuration-components 51 | */ 52 | components: true, 53 | /* 54 | ** Nuxt.js dev-modules 55 | */ 56 | buildModules: [], 57 | /* 58 | ** Nuxt.js modules 59 | */ 60 | modules: [], 61 | /* 62 | ** Build configuration 63 | ** See https://nuxtjs.org/api/configuration-build/ 64 | */ 65 | build: {}, 66 | 67 | env: { 68 | fireBaseAPIKey: "AIzaSyArarycZkVJsySm2HFrlsoys7cuvDe6O5c" 69 | } 70 | }; 71 | -------------------------------------------------------------------------------- /pages/auth/index.vue: -------------------------------------------------------------------------------- 1 | 50 | 51 | 74 | -------------------------------------------------------------------------------- /pages/index.vue: -------------------------------------------------------------------------------- 1 | 44 | 82 | -------------------------------------------------------------------------------- /store/index.js: -------------------------------------------------------------------------------- 1 | import Vuex from "vuex"; 2 | import Cookie from "js-cookie"; 3 | import axios from "axios"; 4 | 5 | const createStore = () => { 6 | return new Vuex.Store({ 7 | state: { 8 | authKey: null, 9 | // expiresIn: null 10 | }, 11 | 12 | mutations: { 13 | setAuthKey(state, authKey) { 14 | state.authKey = authKey; 15 | }, 16 | clearAuthKey(state) { 17 | Cookie.remove("authKey"); 18 | Cookie.remove("expiresIn"); 19 | 20 | if (process.client) { 21 | localStorage.removeItem("authKey"); 22 | localStorage.removeItem("expiresIn"); 23 | } 24 | state.authKey = null; 25 | } 26 | }, 27 | 28 | actions: { 29 | nuxtServerInit(vuexContext, context) {}, 30 | initAuth(vuexContext, req) { 31 | let token; 32 | let expiresIn; 33 | 34 | if (req) { 35 | // server 36 | if (!req.headers.cookie) { 37 | return; 38 | } 39 | 40 | // token aliriq cookie'den 41 | token = req.headers.cookie 42 | .split(";") 43 | .find(c => c.trim().startsWith("authKey=")); 44 | if (token) { 45 | token = token.split("=")[1]; 46 | } 47 | 48 | expiresIn = req.headers.cookie.split(";").find(exp => exp.trim().startsWith("expiresIn=")) 49 | if(expiresIn) { 50 | expiresIn = expiresIn.split("=")[1] 51 | } 52 | } 53 | 54 | else { 55 | // client 56 | token = localStorage.getItem("authKey"); 57 | expiresIn = localStorage.getItem("expiresIn"); 58 | if ( new Date().getTime() > +expiresIn || !token) { 59 | vuexContext.commit("clearAuthKey"); 60 | } 61 | } 62 | vuexContext.commit("setAuthKey", token); 63 | }, 64 | authUser(vuexContext, authData) { 65 | let authLink = "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key="; 66 | 67 | if (authData.isUser) { 68 | authLink = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=" 69 | } 70 | 71 | return axios.post(authLink + process.env.fireBaseAPIKey, 72 | {email: authData.user.email, password: authData.user.password, returnSecureToken: true} 73 | ).then(res => { 74 | console.log(res); 75 | 76 | // let expiresIn = new Date().getTime() + 5000 ; 77 | let expiresIn = new Date().getTime() + +res.data.expiresIn * 1000 ; 78 | 79 | Cookie.set("authKey", res.data.idToken); 80 | Cookie.set("expiresIn", expiresIn); 81 | 82 | localStorage.setItem("authKey", res.data.idToken); 83 | localStorage.setItem("expiresIn", expiresIn); 84 | 85 | vuexContext.commit("setAuthKey", res.data.idToken); 86 | }) 87 | }, 88 | logout(vuexContext) { 89 | vuexContext.commit("clearAuthKey"); 90 | }, 91 | 92 | }, 93 | 94 | getters: { 95 | isAuthenticated(state) { 96 | return state.authKey != null; 97 | }, 98 | getAuthKey(state) { 99 | return state.authKey; 100 | } 101 | } 102 | }); 103 | }; 104 | 105 | export default createStore; 106 | --------------------------------------------------------------------------------