>本教程在均值堆棧應用程序中通過用戶身份驗證引導您。我們將使用一個常見的架構:一個角度單頁應用程序與node.js,express和mongoDB基於rest api相互作用。
>密鑰身份驗證方面:
>用戶數據(帶有哈希密碼)位於mongodb中。
註冊頁
登錄頁面
個人資料頁面(僅適用於身份驗證的用戶)
(get):檢索個人資料詳細信息。
設置API:
>使用
>工具(使用
>安裝)來創建Express App:express -v pug mean-authentication cd mean-authentication npm i npm i pug@latest npm i mongoose
>,數據庫連接(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
>密碼管理(哈希和鹽):
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)
>和
方法使用>模塊在不直接存儲密碼的情況下安全地處理密碼。 setPassword
方法使用validPassword
軟件包來創建JWTS。
crypto
passport.js用於身份驗證:generateJwt
jsonwebtoken
安裝護照和本地策略:
中的護照:
npm i passport passport-local
updateapi/config/passport.js
>初始化護照作為中間件。
// ... (Passport configuration as described in the original tutorial)
app.js
>使用寄存器,登錄和配置文件處理邏輯,完成
。
使用CLI創建Angular應用: , )。 實施 連接角組件和API:
向Express服務器的請求。 啟動Express服務器和Angular App。 測試註冊,登錄和配置文件訪問。 根據需要添加樣式(請參閱GitHub存儲庫以獲取樣式細節)。
api/controllers/profile.js
/api/profile
生成必要的組件(express-jwt
>,ng new client
register
login
>使用profile
來處理API交互作用,在各個組件中實現寄存器和登錄表單。 將導航欄更新以根據登錄狀態動態顯示“登錄”或用戶的名稱和配置文件鏈接。 使用角路線保護(home
)保護authentication
>路線。 最後,實現配置文件頁面以獲取和顯示從受保護的API路由的用戶詳細信息。 AuthenticationService
>
AuthenticationService
常見問題(常見問題解答):/profile
FAQS部分
以上是平均堆棧:構建具有角和角CLI的應用程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!