[Vue] 13 - Amplify: user authentication
Amazon Cognito
一、Cognito 标签
# 可能先需要 amplify init
$ amplify add auth
--> Default configuration
$ amplify push
-
安装库、包
$ sudo npm install aws-amplify aws-amplify-vue
$ sudo npm install -g @aws-amplify/cli
node 12可能会导致安装失败,请尝试node 10稳定版本为宜。
在Ubuntu 18.04系统中安装Node.js 10的方法
1、要在Ubuntu 18.04/Ubuntu 16.04/Debian 9中安装Node.js 10.x LTS,请添加NodeSource二进制分发存储库:
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash
此命令将Node.js APT存储库配置到你的操作系统。
2、添加存储库后,使用apt包管理器在Ubuntu 18.04/Ubuntu 16.04/Debian 9中安装Node.js 10 LTS:
sudo apt update sudo apt -y install gcc g++ make sudo apt -y install nodejs
总结,AWS提供了 “标签”,直接使用即可,有界面有功能。
二、示范例子
代码:https://github.com/ErikCH/Aws-auth-example
视频:Learn Vue.js With Authentication Using Custom AWS Login Pages
$ amplify init
$ amplify add auth
$ amplify push
-
目录结构
-
HelloWorld.vue
这里自定义了输入框的UI。
<template> <div class="hello"> <div v-if="!signedIn"> <!-- <amplify-authenticator></amplify-authenticator> --> <input v-model="login" type="text" name="" placeholder="Login" ><br> <input v-model="password" type="password" name="" placeholder="Password" ><br> <button @click="signIn">Sign in</button> </div> <div v-if="signedIn"> <!-- <amplify-sign-out></amplify-sign-out> --> <button @click="signOut">Sign Out</button> </div> </div> </template>
<script> import { Auth } from 'aws-amplify' import { AmplifyEventBus } from 'aws-amplify-vue';
export default { name: 'HelloWorld', data() { return { login: '', password: '' // signedIn: false } }, props: { msg: String, }, created(){ this.findUser(); AmplifyEventBus.$on('authState', info => { if(info === "signedIn") { this.findUser(); } else { this.$store.state.signedIn = false; // this.signedIn = false; this.$store.state.user = null; } }); }, computed: { signedIn(){ return this.$store.state.signedIn; } }, methods: { signIn(){ Auth.signIn(this.login, this.password) .then(user =>{ this.$store.state.signedIn = !!user; this.$store.state.user = user; } ) .catch(err => console.log(err)); }, signOut() { Auth.signOut() .then(data =>{ this.$store.state.signedIn = !!data; } ) .catch(err => console.log(err)); }, async findUser() { try { const user = await Auth.currentAuthenticatedUser(); // this.signedIn = true; this.$store.state.signedIn = true; this.$store.state.user = user; console.log(user); } catch(err) { // this.signedIn = false; this.$store.state.signedIn = false; this.$store.state.user = null; } } } } </script>
<!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped lang="scss">... ... </style>
-
“注册” 页面设计
两步走:(1) 注册,申请confirmation code (2)验证 code。
<template> <div> <div v-if="!user"> <h1>Sign Up</h1> <input v-model="login" type="text" placeholder="Login"><br> <input v-model="password" type="password" placeholder="Password"> <br> <input v-model="email" type="email" placeholder="Email"><br> <button @click="submit">Submit</button> </div> <div v-if="user"> <h2>Confirm Sign Up</h2> <input v-model="code" type="text" placeholder="Code"><br> <button @click="confirm">Submit</button> </div> </div> </template>
<script> import { Auth } from 'aws-amplify'; export default { data(){ return { login: '', email: '', password: '', code: '', user: '' } }, methods: { confirm() { // After retrieveing the confirmation code from the user Auth.confirmSignUp(this.login, this.code, { // Optional. Force user confirmation irrespective of existing alias. By default set to True. forceAliasCreation: true }).then(data => this.$router.push("/")) .catch(err => console.log(err)); }, submit(){ Auth.signUp({ username: this.login, password: this.password, attributes: { email: this.email }, validationData: [], // optional }) .then(data => this.user = data.user) .catch(err => console.log(err)); } } } </script>
<style> input { margin: 10px; padding: 16px; } </style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律