>本教程在均值堆栈应用程序中通过用户身份验证引导您。我们将使用一个常见的架构:一个角度单页应用程序与node.js,express和mongoDB基于rest api相互作用。
>密钥身份验证方面:
>用户数据(带有哈希密码)位于mongodb中。
注册页
登录页面
个人资料页面(仅适用于身份验证的用户)
(get):检索个人资料详细信息。
设置API:
>使用
>工具(使用
>安装)来创建Express App:<code class="language-bash">express -v pug mean-authentication cd mean-authentication npm i npm i pug@latest npm i mongoose</code>如原始教程中所述,创建必要的目录结构和文件。 提供了
>,数据库连接(app.js
),API路由(api/models/db.js
)和初始控制器stubs(api/routes/index.js
>和api/controllers/authentication.js
)。
带有猫鼬的mongodb架构:api/controllers/profile.js
文件使用mongoose来定义mongodb架构:
api/models/users.js
>密码管理(哈希和盐):
<code class="language-javascript">const mongoose = require('mongoose'); const crypto = require('crypto'); const jwt = require('jsonwebtoken'); // ... (Schema definition and setPassword, validPassword, generateJwt methods as described in the original tutorial)</code>>
>和
方法使用>模块在不直接存储密码的情况下安全地处理密码。 setPassword
方法使用validPassword
软件包来创建JWTS。
crypto
passport.js用于身份验证:generateJwt
jsonwebtoken
安装护照和本地策略:
中的护照:
<code class="language-bash">npm i passport passport-local</code>
updateapi/config/passport.js
>初始化护照作为中间件。
<code class="language-javascript">// ... (Passport configuration as described in the original tutorial)</code>> API端点配置:
app.js
>使用寄存器,登录和配置文件处理逻辑,完成
。
使用CLI创建Angular应用: , )。 实施 连接角组件和API:
向Express服务器的请求。 启动Express服务器和Angular App。 测试注册,登录和配置文件访问。 根据需要添加样式(请参阅GitHub存储库以获取样式细节)。
api/controllers/profile.js
/api/profile
生成必要的组件(express-jwt
>,<code class="language-bash">ng new client</code>
register
login
>使用profile
来处理API交互作用,在各个组件中实现寄存器和登录表单。 将导航栏更新以根据登录状态动态显示“登录”或用户的名称和配置文件链接。 使用角路线保护(home
)保护authentication
>路线。 最后,实现配置文件页面以获取和显示从受保护的API路由的用户详细信息。AuthenticationService
>
AuthenticationService
常见问题(常见问题解答):/profile
FAQS部分
以上是平均堆栈:构建具有角和角CLI的应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!