├── .browserslistrc ├── .eslintrc.js ├── .gitignore ├── README.md ├── babel.config.js ├── package-lock.json ├── package.json ├── public ├── favicon.ico └── index.html └── src ├── App.vue ├── assets └── logo.png ├── components └── HelloWorld.vue ├── firebase └── index.js ├── main.js ├── router └── index.js ├── store └── index.js └── views ├── About.vue ├── Home.vue └── Login.vue /.browserslistrc: -------------------------------------------------------------------------------- 1 | > 1% 2 | last 2 versions 3 | not dead 4 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | env: { 4 | node: true 5 | }, 6 | 'extends': [ 7 | 'plugin:vue/vue3-essential', 8 | 'eslint:recommended' 9 | ], 10 | parserOptions: { 11 | parser: 'babel-eslint' 12 | }, 13 | rules: { 14 | 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 15 | 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off' 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | 5 | 6 | # local env files 7 | .env.local 8 | .env.*.local 9 | 10 | # Log files 11 | npm-debug.log* 12 | yarn-debug.log* 13 | yarn-error.log* 14 | pnpm-debug.log* 15 | 16 | # Editor directories and files 17 | .idea 18 | .vscode 19 | *.suo 20 | *.ntvs* 21 | *.njsproj 22 | *.sln 23 | *.sw? 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # yt 2 | 3 | ## Project setup 4 | ``` 5 | npm install 6 | ``` 7 | 8 | ### Compiles and hot-reloads for development 9 | ``` 10 | npm run serve 11 | ``` 12 | 13 | ### Compiles and minifies for production 14 | ``` 15 | npm run build 16 | ``` 17 | 18 | ### Lints and fixes files 19 | ``` 20 | npm run lint 21 | ``` 22 | 23 | ### Customize configuration 24 | See [Configuration Reference](https://cli.vuejs.org/config/). 25 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: [ 3 | '@vue/cli-plugin-babel/preset' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yt", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "serve": "vue-cli-service serve", 7 | "build": "vue-cli-service build", 8 | "lint": "vue-cli-service lint" 9 | }, 10 | "dependencies": { 11 | "core-js": "^3.6.5", 12 | "firebase": "^9.6.3", 13 | "vue": "^3.0.0", 14 | "vue-router": "^4.0.0-0", 15 | "vuex": "^4.0.0-0" 16 | }, 17 | "devDependencies": { 18 | "@vue/cli-plugin-babel": "~4.5.0", 19 | "@vue/cli-plugin-eslint": "~4.5.0", 20 | "@vue/cli-plugin-router": "~4.5.0", 21 | "@vue/cli-plugin-vuex": "~4.5.0", 22 | "@vue/cli-service": "~4.5.0", 23 | "@vue/compiler-sfc": "^3.0.0", 24 | "babel-eslint": "^10.1.0", 25 | "eslint": "^6.7.2", 26 | "eslint-plugin-vue": "^7.0.0" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TylerPottsDev/yt-vuejs-firebase9-auth/e4b1edfd2f3673262a3fa8c24e5896482dec16a5/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | <%= htmlWebpackPlugin.options.title %> 9 | 10 | 11 | 14 |
15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/App.vue: -------------------------------------------------------------------------------- 1 | 9 | 10 | 24 | 25 | 52 | -------------------------------------------------------------------------------- /src/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TylerPottsDev/yt-vuejs-firebase9-auth/e4b1edfd2f3673262a3fa8c24e5896482dec16a5/src/assets/logo.png -------------------------------------------------------------------------------- /src/components/HelloWorld.vue: -------------------------------------------------------------------------------- 1 | 34 | 35 | 43 | 44 | 45 | 61 | -------------------------------------------------------------------------------- /src/firebase/index.js: -------------------------------------------------------------------------------- 1 | import { initializeApp } from "firebase/app" 2 | import { getAuth } from "firebase/auth" 3 | 4 | const firebaseConfig = { 5 | apiKey: "AIzaSyB3Z8T6I_ZZ9fnXnUUGDL9-3ocJVII3wEM", 6 | authDomain: "vue-fire-auth-yt.firebaseapp.com", 7 | projectId: "vue-fire-auth-yt", 8 | storageBucket: "vue-fire-auth-yt.appspot.com", 9 | messagingSenderId: "745571531497", 10 | appId: "1:745571531497:web:31ef49d3de839b54030e78" 11 | } 12 | 13 | const app = initializeApp(firebaseConfig) 14 | 15 | const auth = getAuth(app) 16 | 17 | export { auth } -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | import { createApp } from 'vue' 2 | import App from './App.vue' 3 | import router from './router' 4 | import store from './store' 5 | 6 | createApp(App).use(store).use(router).mount('#app') 7 | -------------------------------------------------------------------------------- /src/router/index.js: -------------------------------------------------------------------------------- 1 | import { createRouter, createWebHistory } from 'vue-router' 2 | import Home from '../views/Home.vue' 3 | import Login from '../views/Login.vue' 4 | import { auth } from '../firebase' 5 | 6 | const routes = [ 7 | { 8 | path: '/', 9 | name: 'Home', 10 | component: Home, 11 | meta: { 12 | requiresAuth: true 13 | } 14 | }, 15 | { 16 | path: '/about', 17 | name: 'About', 18 | component: () => import('../views/About.vue'), 19 | meta: { 20 | requiresAuth: true 21 | } 22 | }, 23 | { 24 | path: '/login', 25 | name: 'Login', 26 | component: Login 27 | } 28 | ] 29 | 30 | const router = createRouter({ 31 | history: createWebHistory(process.env.BASE_URL), 32 | routes 33 | }) 34 | 35 | router.beforeEach((to, from, next) => { 36 | if (to.path === '/login' && auth.currentUser) { 37 | next('/') 38 | return; 39 | } 40 | 41 | if (to.matched.some(record => record.meta.requiresAuth) && !auth.currentUser) { 42 | next('/login') 43 | return; 44 | } 45 | 46 | next(); 47 | }) 48 | 49 | export default router 50 | -------------------------------------------------------------------------------- /src/store/index.js: -------------------------------------------------------------------------------- 1 | import { createStore } from 'vuex' 2 | import router from '../router' 3 | import { auth } from '../firebase' 4 | import { 5 | createUserWithEmailAndPassword, 6 | signInWithEmailAndPassword, 7 | signOut 8 | } from 'firebase/auth' 9 | 10 | export default createStore({ 11 | state: { 12 | user: null 13 | }, 14 | mutations: { 15 | 16 | SET_USER (state, user) { 17 | state.user = user 18 | }, 19 | 20 | CLEAR_USER (state) { 21 | state.user = null 22 | } 23 | 24 | }, 25 | actions: { 26 | async login ({ commit }, details) { 27 | const { email, password } = details 28 | 29 | try { 30 | await signInWithEmailAndPassword(auth, email, password) 31 | } catch (error) { 32 | switch(error.code) { 33 | case 'auth/user-not-found': 34 | alert("User not found") 35 | break 36 | case 'auth/wrong-password': 37 | alert("Wrong password") 38 | break 39 | default: 40 | alert("Something went wrong") 41 | } 42 | 43 | return 44 | } 45 | 46 | commit('SET_USER', auth.currentUser) 47 | 48 | router.push('/') 49 | }, 50 | 51 | async register ({ commit}, details) { 52 | const { email, password } = details 53 | 54 | try { 55 | await createUserWithEmailAndPassword(auth, email, password) 56 | } catch (error) { 57 | switch(error.code) { 58 | case 'auth/email-already-in-use': 59 | alert("Email already in use") 60 | break 61 | case 'auth/invalid-email': 62 | alert("Invalid email") 63 | break 64 | case 'auth/operation-not-allowed': 65 | alert("Operation not allowed") 66 | break 67 | case 'auth/weak-password': 68 | alert("Weak password") 69 | break 70 | default: 71 | alert("Something went wrong") 72 | } 73 | 74 | return 75 | } 76 | 77 | commit('SET_USER', auth.currentUser) 78 | 79 | router.push('/') 80 | }, 81 | 82 | async logout ({ commit }) { 83 | await signOut(auth) 84 | 85 | commit('CLEAR_USER') 86 | 87 | router.push('/login') 88 | }, 89 | 90 | fetchUser ({ commit }) { 91 | auth.onAuthStateChanged(async user => { 92 | if (user === null) { 93 | commit('CLEAR_USER') 94 | } else { 95 | commit('SET_USER', user) 96 | 97 | if (router.isReady() && router.currentRoute.value.path === '/login') { 98 | router.push('/') 99 | } 100 | } 101 | }) 102 | } 103 | 104 | } 105 | }) 106 | -------------------------------------------------------------------------------- /src/views/About.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /src/views/Home.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /src/views/Login.vue: -------------------------------------------------------------------------------- 1 | 38 | 39 | 66 | 67 | --------------------------------------------------------------------------------