adding latest

This commit is contained in:
2020-03-16 02:41:23 +00:00
parent 0f33f70ee4
commit 3334659bec
7 changed files with 153 additions and 54 deletions

View File

@@ -26,6 +26,5 @@ Libraries that work with Vue:
JS library to simplify working with arrays, numbers, objects, strings, etc.
- Iterating arrays, objects, & strings
- Manipulating & testing values
- Creating composite functions
- Document steps of creating js files for aws

View File

@@ -1,15 +1,18 @@
import Vue from "vue";
import VueRouter from "vue-router";
// import Home from "../views/Home.vue";
import Home from "../views/Home.vue";
import { AmplifyEventBus } from "aws-amplify-vue";
import { getUser } from "@/utils/auth.js";
Vue.use(VueRouter);
const routes = [
// {
// path: "/",
// name: "Home",
// component: Home
// },
{
path: "/",
name: "Home",
component: Home,
meta: { requiresAuth: true }
},
{
path: "/about",
name: "About",
@@ -23,19 +26,22 @@ const routes = [
path: "/signUp",
name: "signUp",
component: () =>
import(/* webpackChunkName: "signup" */ "../views/SignUp.vue")
import(/* webpackChunkName: "signup" */ "../views/SignUp.vue"),
meta: { requiresAuth: false }
},
{
path: "/signUpConfirm",
name: "signUpConfirm",
component: () =>
import(/* webpackChunkName: "confirm" */ "../views/SignUpConfirm.vue")
import(/* webpackChunkName: "confirm" */ "../views/SignUpConfirm.vue"),
meta: { requiresAuth: false }
},
{
path: "/signIn",
name: "signIn",
component: () =>
import(/* webpackChunkName: "signin" */ "../views/SignIn.vue")
import(/* webpackChunkName: "signin" */ "../views/SignIn.vue"),
meta: { requiresAuth: false }
}
];
@@ -45,4 +51,59 @@ const router = new VueRouter({
routes
});
getUser().then(user => {
console.log("router getting user.")
if (user) {
router.push({ path: "/" });
}
});
AmplifyEventBus.$on("authState", async state => {
const pushPathes = {
signedOut: () => {
router.push({ path: "/signIn" });
},
signUp: () => {
router.push({ path: "/signUp" });
},
confirmSignUp: () => {
router.push({ path: "/signUpConfirm" });
},
signIn: () => {
router.push({ path: "/signIn" });
},
signedIn: () => {
router.push({ path: "/" });
}
};
if (typeof pushPathes[state] === "function") {
pushPathes[state]();
}
});
router.beforeResolve(async (to, from, next) => {
console.log("router before resolve getting current user.")
const user = await getUser();
if (!user) {
if (to.matched.some(record => record.meta.requiresAuth)) {
return next({
path: "/signIn"
});
}
} else {
if (
to.matched.some(
record =>
typeof record.meta.requiresAuth === "boolean" &&
!record.meta.requiresAuth
)
) {
return next({
path: "/"
});
}
}
return next();
});
export default router;

View File

@@ -1,15 +1,39 @@
import Vue from 'vue'
import Vuex from 'vuex'
import Vue from "vue";
import Vuex from "vuex";
import { Auth } from "aws-amplify";
Vue.use(Vuex)
Vue.use(Vuex);
export default new Vuex.Store({
state: {
authorized: false,
user: null
},
mutations: {
user(state, user) {
state.authorized =
!!user && user.attributes && user.attributes.email_verified;
state.user = user;
}
},
actions: {
},
modules: {
async fetchUser({ commit }) {
try {
const user = await Auth.currentAuthenticatedUser()
const expires =
user.getSignInUserSession().getIdToken().payload.exp -
Math.floor(new Date().getTime() / 1000);
console.log(`Token expires in ${expires} seconds.`);
commit("user", user);
} catch (err) {
commit("user", null);
}
})
}
},
modules: {},
getters: {
userEmail: state => {
return state.user.attributes.email;
}
}
});

View File

@@ -1,10 +1,12 @@
import { Auth } from "aws-amplify";
import { AmplifyEventBus } from "aws-amplify-vue";
function getUser() {
return Auth.currentAuthenticatedUser()
async function getUser() {
console.log("getting current user")
return await Auth.currentAuthenticatedUser()
.then(user => {
if (user && user.signInUserSession) {
console.log("returning current signed in user")
return user;
} else {
return null;
@@ -15,8 +17,8 @@ function getUser() {
});
}
function signUp(username, password) {
return Auth.signUp({
async function signUp(username, password) {
return await Auth.signUp({
username,
password,
attributes: {
@@ -36,8 +38,8 @@ function signUp(username, password) {
});
}
function confirmSignUp(username, code) {
return Auth.confirmSignIn(username, code)
async function confirmSignUp(username, code) {
const user = await Auth.confirmSignUp(username, code)
.then(data => {
AmplifyEventBus.$emit("authState", "signIn");
return data; // successful sign up
@@ -46,6 +48,7 @@ function confirmSignUp(username, code) {
console.log(err);
throw err;
});
console.log(user);
}
function resendSignUp(username) {
@@ -61,7 +64,11 @@ function resendSignUp(username) {
async function signIn(username, password) {
try {
console.log("Attempting sign in");
const user = await Auth.signIn(username, password);
AmplifyEventBus.$emit("authState", "signedIn");
console.log("Signed in in fn");
console.log(user);
if (user) {
AmplifyEventBus.$emit("authState", "signedIn");
}
@@ -94,4 +101,4 @@ function signOut() {
});
}
export {getUser, signUp, confirmSignUp, resendSignUp, signIn, signOut}
export { getUser, signUp, confirmSignUp, resendSignUp, signIn, signOut };

View File

@@ -1,4 +1,7 @@
<template>
<v-container class="grey lighten-5">
<v-row class="mb-6" justify="center" no-gutters>
<v-col lg="2">
<div class="sign-in">
<h1>Sign In</h1>
<v-form v-model="valid" ref="form" lazy-validation>
@@ -23,6 +26,9 @@
<v-btn :disabled="!valid" @click="submit">Submit</v-btn>
</v-form>
</div>
</v-col>
</v-row>
</v-container>
</template>
<script>
@@ -53,12 +59,13 @@ export default {
}
},
methods: {
submit() {
async submit() {
if (this.$refs.form.validate()) {
console.log(
`SIGN IN username: ${this.username}, password: ${this.password}`
);
signIn(this.username, this.password);
await signIn(this.username, this.password);
console.log("Signed in");
}
}
}

View File

@@ -22,7 +22,7 @@
required
>
</v-text-field>
<v-btn :disabled="!valid" @click="submit"></v-btn>
<v-btn :disabled="!valid" @click="submit">Submit</v-btn>
</v-form>
</div>
</template>
@@ -61,6 +61,7 @@ export default {
`SIGN UP username: ${this.username}, password: ${this.password}, email: ${this.username}`
);
signUp(this.username, this.password);
console.log("finished");
}
}
}

View File

@@ -46,15 +46,15 @@ export default {
}
},
methods: {
submit() {
async submit() {
if (this.$refs.form.validate()) {
console.log(`CONFIRM username: ${this.username}, code: ${this.code}`);
confirmSignUp(this.username, this.code)
await confirmSignUp(this.username, this.code);
}
},
resend() {
console.log(`RESEND username: ${this.username}`);
resendSignUp(this.username)
resendSignUp(this.username);
}
}
};