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. JS library to simplify working with arrays, numbers, objects, strings, etc.
- Iterating arrays, objects, & strings
- Manipulating & testing values - Document steps of creating js files for aws
- Creating composite functions

View File

@@ -1,15 +1,18 @@
import Vue from "vue"; import Vue from "vue";
import VueRouter from "vue-router"; 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); Vue.use(VueRouter);
const routes = [ const routes = [
// { {
// path: "/", path: "/",
// name: "Home", name: "Home",
// component: Home component: Home,
// }, meta: { requiresAuth: true }
},
{ {
path: "/about", path: "/about",
name: "About", name: "About",
@@ -23,19 +26,22 @@ const routes = [
path: "/signUp", path: "/signUp",
name: "signUp", name: "signUp",
component: () => component: () =>
import(/* webpackChunkName: "signup" */ "../views/SignUp.vue") import(/* webpackChunkName: "signup" */ "../views/SignUp.vue"),
meta: { requiresAuth: false }
}, },
{ {
path: "/signUpConfirm", path: "/signUpConfirm",
name: "signUpConfirm", name: "signUpConfirm",
component: () => component: () =>
import(/* webpackChunkName: "confirm" */ "../views/SignUpConfirm.vue") import(/* webpackChunkName: "confirm" */ "../views/SignUpConfirm.vue"),
meta: { requiresAuth: false }
}, },
{ {
path: "/signIn", path: "/signIn",
name: "signIn", name: "signIn",
component: () => 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 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; export default router;

View File

@@ -1,15 +1,39 @@
import Vue from 'vue' import Vue from "vue";
import Vuex from 'vuex' import Vuex from "vuex";
import { Auth } from "aws-amplify";
Vue.use(Vuex) Vue.use(Vuex);
export default new Vuex.Store({ export default new Vuex.Store({
state: { state: {
authorized: false,
user: null
}, },
mutations: { mutations: {
user(state, user) {
state.authorized =
!!user && user.attributes && user.attributes.email_verified;
state.user = user;
}
}, },
actions: { actions: {
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: { modules: {},
getters: {
userEmail: state => {
return state.user.attributes.email;
}
} }
}) });

View File

@@ -1,10 +1,12 @@
import { Auth } from "aws-amplify"; import { Auth } from "aws-amplify";
import { AmplifyEventBus } from "aws-amplify-vue"; import { AmplifyEventBus } from "aws-amplify-vue";
function getUser() { async function getUser() {
return Auth.currentAuthenticatedUser() console.log("getting current user")
return await Auth.currentAuthenticatedUser()
.then(user => { .then(user => {
if (user && user.signInUserSession) { if (user && user.signInUserSession) {
console.log("returning current signed in user")
return user; return user;
} else { } else {
return null; return null;
@@ -15,8 +17,8 @@ function getUser() {
}); });
} }
function signUp(username, password) { async function signUp(username, password) {
return Auth.signUp({ return await Auth.signUp({
username, username,
password, password,
attributes: { attributes: {
@@ -36,8 +38,8 @@ function signUp(username, password) {
}); });
} }
function confirmSignUp(username, code) { async function confirmSignUp(username, code) {
return Auth.confirmSignIn(username, code) const user = await Auth.confirmSignUp(username, code)
.then(data => { .then(data => {
AmplifyEventBus.$emit("authState", "signIn"); AmplifyEventBus.$emit("authState", "signIn");
return data; // successful sign up return data; // successful sign up
@@ -46,6 +48,7 @@ function confirmSignUp(username, code) {
console.log(err); console.log(err);
throw err; throw err;
}); });
console.log(user);
} }
function resendSignUp(username) { function resendSignUp(username) {
@@ -61,7 +64,11 @@ function resendSignUp(username) {
async function signIn(username, password) { async function signIn(username, password) {
try { try {
console.log("Attempting sign in");
const user = await Auth.signIn(username, password); const user = await Auth.signIn(username, password);
AmplifyEventBus.$emit("authState", "signedIn");
console.log("Signed in in fn");
console.log(user);
if (user) { if (user) {
AmplifyEventBus.$emit("authState", "signedIn"); 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,28 +1,34 @@
<template> <template>
<div class="sign-in"> <v-container class="grey lighten-5">
<h1>Sign In</h1> <v-row class="mb-6" justify="center" no-gutters>
<v-form v-model="valid" ref="form" lazy-validation> <v-col lg="2">
<v-text-field <div class="sign-in">
v-model="username" <h1>Sign In</h1>
:rules="emailRules" <v-form v-model="valid" ref="form" lazy-validation>
label="Email Address" <v-text-field
required v-model="username"
></v-text-field> :rules="emailRules"
<v-text-field label="Email Address"
v-model="password" required
:append-icon="passwordVisible ? 'mdi-eye' : 'mdi-eye-off'" ></v-text-field>
:rules="[passwordRules.required, passwordRules.min]" <v-text-field
:type="passwordVisible ? 'text' : 'password'" v-model="password"
name="password" :append-icon="passwordVisible ? 'mdi-eye' : 'mdi-eye-off'"
label="Password" :rules="[passwordRules.required, passwordRules.min]"
hint="At least 8 characters" :type="passwordVisible ? 'text' : 'password'"
counter name="password"
@click:append="passwordVisible = !passwordVisible" label="Password"
required hint="At least 8 characters"
></v-text-field> counter
<v-btn :disabled="!valid" @click="submit">Submit</v-btn> @click:append="passwordVisible = !passwordVisible"
</v-form> required
</div> ></v-text-field>
<v-btn :disabled="!valid" @click="submit">Submit</v-btn>
</v-form>
</div>
</v-col>
</v-row>
</v-container>
</template> </template>
<script> <script>
@@ -53,12 +59,13 @@ export default {
} }
}, },
methods: { methods: {
submit() { async submit() {
if (this.$refs.form.validate()) { if (this.$refs.form.validate()) {
console.log( console.log(
`SIGN IN username: ${this.username}, password: ${this.password}` `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 required
> >
</v-text-field> </v-text-field>
<v-btn :disabled="!valid" @click="submit"></v-btn> <v-btn :disabled="!valid" @click="submit">Submit</v-btn>
</v-form> </v-form>
</div> </div>
</template> </template>
@@ -61,6 +61,7 @@ export default {
`SIGN UP username: ${this.username}, password: ${this.password}, email: ${this.username}` `SIGN UP username: ${this.username}, password: ${this.password}, email: ${this.username}`
); );
signUp(this.username, this.password); signUp(this.username, this.password);
console.log("finished");
} }
} }
} }

View File

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