Node.js에서 비밀번호 백업 시스템을 만드는 방법

발행: (2026년 1월 14일 오전 03:01 GMT+9)
4 min read
원문: Dev.to

Source: Dev.to

소개

비밀번호는 자주 잊어버리며, 매번 이메일을 통해 비밀번호를 재설정하도록 강요하면 사용자가 불편함을 느낍니다. 매직 링크는 비밀번호를 안전하고 원활하게 백업할 수 있는 방법을 제공합니다. 이 튜토리얼에서는 Node.jsauth-verify 라이브러리를 사용해 비밀번호 백업 시스템을 구현하는 방법을 보여줍니다.

설치

npm install auth-verify express
  • auth-verify – 매직 링크 생성 및 검증을 담당합니다.
  • express – 서버와 라우트를 생성합니다.

서버 초기화

const express = require('express');
const app = express();

const AuthVerify = require('auth-verify');
const auth = new AuthVerify({
  mlSecret: 'super_secret_key',   // 매직 링크 서명을 위한 비밀키
  mlExpiry: '5m',                // 링크 만료 시간
  appUrl: 'http://localhost:3000', // 앱의 기본 URL
  storeTokens: 'memory'          // 토큰을 저장할 위치 ('memory' 또는 'redis')
});

// 매직‑링크 이메일 발송자 설정
auth.magic.sender({
  service: 'gmail',
  sender: 'yourapp@gmail.com',
  pass: 'your_gmail_app_password'
});

매직 링크 전송

사용자가 비밀번호를 잊어버렸을 때, 이메일 주소로 매직 링크를 전송합니다:

app.post('/send-magic-link', async (req, res) => {
  const { email } = req.body;
  try {
    const result = await auth.magic.send(email, {
      subject: 'Your Reset Password Link ✨',
      html: `
Click below to sign in:

             [Login Now]({{link}})` // {{link}} is replaced with a URL like
                                            // http://localhost:3000/auth/verify?token=GENERATED_TOKEN
    });
    res.json({ success: true, message: 'Magic link sent!', result });
  } catch (err) {
    console.error(err);
    res.status(500).json({ success: false, message: 'Failed to send magic link' });
  }
});

매직 링크 검증

사용자가 링크를 클릭하면 토큰을 검증하고 비밀번호 재설정 폼을 표시합니다:

app.get('/auth/verify', async (req, res) => {
  const { token } = req.query; // token extracted from the URL
  try {
    await auth.magic.verify(token);
    res.send(`
      
        
        Set new password
      
    `);
  } catch (err) {
    res.status(400).json({ success: false, message: err.message });
  }
});

비밀번호 재설정

폼 제출을 처리하고 사용자의 비밀번호를 업데이트합니다 (구현은 사용자 저장소에 따라 다릅니다):

app.post('/reset-password', (req, res) => {
  const { newpassword } = req.body; // new password supplied by the user
  // TODO: Save `newpassword` for the corresponding user in your database
  res.json({ success: true, message: 'Password has been updated.' });
});

결론

auth-verify와 매직 링크를 활용하면 사용자가 기존 비밀번호를 기억하지 못해도 비밀번호를 재설정할 수 있습니다. 이 방법은 Node.js 애플리케이션에 사용자 친화적이고 안전한 비밀번호 백업 시스템을 제공합니다.

Back to Blog

관련 글

더 보기 »