...
/Integrating User Authentication and Protecting Routes
Integrating User Authentication and Protecting Routes
Learn how to add user authentication functionality in Vue.js application.
Introduction
We have created our frontend UI as well as the backend UI. Now we can integrate and make the frontend function. The process we are going to implement is as follows:
We now have a plan to integrate our backend and frontend. To begin with, we need to install the aws-amplify
package. We run the following command to install the AWS Amplify library:
npm install aws-amplify
Once installed, we need to update the ./main.js
file with the following:
import Vue from "vue";import App from "./App.vue";import router from "./router";import store from "./store";import "./assets/index.css"Vue.config.productionTip = false;// amplify settingsimport Amplify from 'aws-amplify';import awsconfig from './aws-exports';Amplify.configure(awsconfig);new Vue({router,store,render: (h) => h(App),}).$mount("#app");
That is all we have to do to configure our Vue.js application with the AWS Amplify library.
Adding authentication
Let’s begin by creating a folder called lib
and creating a file called auth.js
. Then we add the following contents:
import { Auth } from 'aws-amplify';// check user authentication statusexport const checkUserStatus = async () => {try {const user = await Auth.currentAuthenticatedUser()return user} catch (error) {throw new Error(error)}}// signinexport const signIn = async (username, password) => {try {const user = await Auth.signIn(username, password);return user} catch (error) {throw new Error(error)}}// signupexport const signUp = async (email, phoneNumber, password) => {try {const { user } = await Auth.signUp({username: email,password: password,attributes: {email: email,'custom:phoneNumber': phoneNumber}});return user} catch (error) {throw new Error(error)}}// confirm accountexport const confirmSignUp = async (username, code) => {try {await Auth.confirmSignUp(username, code);} catch (error) {throw new Error(error)}}// resend confirmation codeexport const resendConfirmationCode = async (username) => {try {await Auth.resendSignUp(username);} catch (err) {throw new Error(error)}}// signoutexport const signOut = async () => {try {await Auth.signOut();} catch (error) {console.log(error);}}// global sign out// this will signout user from all devicesexport const globalSignOut = async () => {try {await Auth.signOut({ global: true });} catch (error) {console.log(error);}}// Send confirmation code to user's emailexport const sendResetCodePassword = async (username) => {try {const code = await Auth.forgotPassword(username)return code} catch (error) {throw new Error(error)}}// reset user passwordexport const resetPassword = async (username, code, new_password) => {try {await Auth.forgotPasswordSubmit(username, code, new_password)} catch (error) {throw new Error(error)}}
We now have all the functions that are needed to complete the following actions:
- Register a new user.
- Confirm user email.
- Log in to the application.
- Set up a forgotten password mechanism–this