Bcrypt和PassportJs用户注册
创始人
2024-11-26 22:31:06
0次

要实现使用Bcrypt和PassportJs进行用户注册,可以按照以下步骤进行:

  1. 安装所需的依赖:
npm install bcrypt passport passport-local express-session
  1. 创建一个名为app.js的主文件,并添加以下代码:
const express = require("express");
const session = require("express-session");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcrypt");

const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(session({ secret: "secret", resave: false, saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());

// 模拟一个用户数据库
const users = [];

// 配置Passport本地策略
passport.use(
  new LocalStrategy((username, password, done) => {
    const user = users.find((user) => user.username === username);
    if (!user) {
      return done(null, false, { message: "Incorrect username." });
    }
    bcrypt.compare(password, user.password, (err, res) => {
      if (res) {
        return done(null, user);
      } else {
        return done(null, false, { message: "Incorrect password." });
      }
    });
  })
);

// 序列化和反序列化用户
passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  const user = users.find((user) => user.id === id);
  done(null, user);
});

// 注册路由
app.get("/", (req, res) => {
  res.send("Home Page");
});

app.get("/register", (req, res) => {
  res.send(`
    

Register



`); }); app.post("/register", (req, res) => { const { username, password } = req.body; const hashedPassword = bcrypt.hashSync(password, 10); const newUser = { id: Date.now().toString(), username, password: hashedPassword }; users.push(newUser); res.redirect("/login"); }); app.get("/login", (req, res) => { res.send(`

Login



`); }); app.post( "/login", passport.authenticate("local", { successRedirect: "/profile", failureRedirect: "/login", }) ); app.get("/profile", isAuthenticated, (req, res) => { res.send(`

Welcome, ${req.user.username}!

`); }); app.get("/logout", (req, res) => { req.logout(); res.redirect("/"); }); // 中间件函数,用于检查用户是否已经登录 function isAuthenticated(req, res, next) { if (req.isAuthenticated()) { return next(); } res.redirect("/login"); } // 启动服务器 app.listen(3000, () => { console.log("Server started on port 3000"); });
  1. 运行应用程序:
node app.js

现在,你可以在浏览器中访问http://localhost:3000/register来注册新用户,然后使用注册的用户名和密码登录。登录后,你将被重定向到http://localhost:3000/profile,显示欢迎消息。你也可以在浏览器中访问http://localhost:3000/logout来注销用户。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...