diff --git a/login/app.js b/login/app.js index 1ba443c704982aa791dcadfbe8e5fdd86050fc63..9acbbc32646344974a9d699c0d9861a4a758e354 100644 --- a/login/app.js +++ b/login/app.js @@ -2,6 +2,10 @@ const express = require('express'); const mongoose = require('mongoose'); const session = require('express-session'); const passport = require('./config/passport'); +//鍮꾨�踰덊샇 蹂�寃� �쇱슦�� �깅줉 +const passwordRoutes = require('./route/conf_password'); +//�됰꽕�� 蹂�寃� �쇱슦�� �깅줉 +const confNameRoutes = require('./route/conf_name.js'); //鍮꾨�踰덊샇 �뷀샇�붿슜 紐⑤뱢 const bcrypt = require('bcrypt'); //User define @@ -10,10 +14,16 @@ const User = require('./models/user'); const app = express(); // JSON �붿껌 蹂몃Ц�� �뚯떛�섎뒗 誘몃뱾�⑥뼱 異붽� app.use(express.json()); +//鍮꾨�踰덊샇 蹂�寃� �쇱슦�� 異붽� +app.use('/conf_password',passwordRoutes); +//�대쫫 蹂�寃� �쇱슦�� +app.use('/conf_name', confNameRoutes); + + // MongoDB �곌껐 -mongoose.connect('mongodb://localhost:27017/googleauth', { +mongoose.connect('mongodb://localhost:27017/logininfo', { useNewUrlParser: true, useUnifiedTopology: true, }).then(() => console.log('MongoDB �곌껐 �깃났')) @@ -23,7 +33,7 @@ mongoose.connect('mongodb://localhost:27017/googleauth', { app.use(session({ secret: 'your_secret_key', resave: false, - saveUninitialized: false + saveUninitialized: false, })); // Passport 珥덇린�� @@ -53,12 +63,18 @@ app.get('/logout', (req, res) => { // ���쒕낫�� app.get('/dashboard', (req, res) => { - // if (req.isAuthenticated()) { - // res.send(`<h1>Welcome, ${req.user.displayName}</h1><a href="/logout">Logout</a>`); - // } else { - // res.redirect('/'); - // } - res.send(`<h1>Welcome</h1><a href="/logout">Logout</a>`); + + const hasPassword = !!req.user.password; // 鍮꾨�踰덊샇媛� �덈뒗吏� �뺤씤 + const userName = req.user.name || req.user.email || 'Guest'; + + res.send(` + <h1>Welcome, ${req.user.name || req.user.email}</h1> + <a href="/conf_name/change">�ъ슜�� �대쫫 蹂�寃�</a> + <br> + ${hasPassword ? '<a href="/conf_password/change">鍮꾨�踰덊샇 蹂�寃�</a>' : ''} + <br> + <a href="/logout">Logout</a> + `); }); // // �� @@ -74,6 +90,15 @@ app.get('/signup', (req, res) => { res.sendFile(__dirname + '/public/signup.html'); // login.html �뚯씪 寃쎈줈 �ㅼ젙 }); +app.get('/conf_password', (req, res) => { + + if (req.isAuthenticated && req.isAuthenticated()) { // 濡쒓렇�� �щ� �뺤씤 + res.sendFile(__dirname + '/public/conf_password.html'); + } +}); + + + //�� - index.html app.use(express.static('public')); @@ -100,8 +125,15 @@ app.post('/login', async (req, res) => { return res.status(400).json({ message: '鍮꾨�踰덊샇媛� �쇱튂�섏� �딆뒿�덈떎.' }); } - res.status(200).json({ message: '濡쒓렇�� �깃났' }); - console.log('濡쒓렇�� �깃났: ',user); + // 濡쒓렇�� �깃났 �� �ъ슜�� �몄뀡�� ���� + req.login(user, (err) => { + if (err) { + console.error('濡쒓렇�� �몄뀡 ���� �ㅻ쪟:', err); + return res.status(500).json({ message: '濡쒓렇�� �몄뀡 ���� 以� �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.' }); + } + res.status(200).json({ message: '濡쒓렇�� �깃났' }); + console.log('濡쒓렇�� �깃났: ', user); + }); } catch (error) { console.error('濡쒓렇�� �ㅻ쪟:', error); res.status(500).json({ message: '�쒕쾭 �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.' }); diff --git a/login/public/conf_name.html b/login/public/conf_name.html new file mode 100644 index 0000000000000000000000000000000000000000..69f389c0da949b81353e599471eaeebe8cb2e602 --- /dev/null +++ b/login/public/conf_name.html @@ -0,0 +1,40 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Change Name</title> +</head> +<body> + <h2>�대쫫 蹂�寃�</h2> + <form id="name-change-form"> + <input type="text" id="name" placeholder="Name" required> + <input type="text" id="new-name" placeholder="New Name" required> + <button type="submit">�대쫫 蹂�寃�</button> + </form> + <script> + document.getElementById('name-change-form').addEventListener('submit', async (event) => { + event.preventDefault(); + const name = document.getElementById('name').value; + const newName = document.getElementById('new-name').value; + + try { + const response = await fetch('/conf_name/change', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ name, newName}) + }); + + const data = await response.json(); + alert(data.message); + + if (response.ok) { + location.href = '/dashboard'; // 蹂�寃� �깃났 �� ���쒕낫�쒕줈 �대룞 + } + } catch (error) { + console.error('�대쫫 蹂�寃� �붿껌 以� �ㅻ쪟:', error); + alert('�대쫫 蹂�寃� 以� �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.'); + } + }); + </script> +</body> +</html> diff --git a/login/public/conf_password.html b/login/public/conf_password.html new file mode 100644 index 0000000000000000000000000000000000000000..e92b2ceb7d295dd63bae90d8ccbee879c79b42db --- /dev/null +++ b/login/public/conf_password.html @@ -0,0 +1,42 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Change Password</title> +</head> +<body> + <h2>鍮꾨�踰덊샇 蹂�寃�</h2> + <form id="password-change-form"> + <input type="email" id="email" placeholder="Email" required> + <input type="password" id="old-password" placeholder="Old Password" required> + <input type="password" id="new-password" placeholder="New Password" required> + <button type="submit">鍮꾨�踰덊샇 蹂�寃�</button> + </form> + <script> + document.getElementById('password-change-form').addEventListener('submit', async (event) => { + event.preventDefault(); + const email = document.getElementById('email').value; + const oldPassword = document.getElementById('old-password').value; + const newPassword = document.getElementById('new-password').value; + + try { + const response = await fetch('/conf_password/change', { // POST �붿껌 + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ email, oldPassword, newPassword }) + }); + + const data = await response.json(); + alert(data.message); // �쒕쾭 �묐떟 硫붿떆吏� �쒖떆 + + if (response.ok) { + location.href = '/dashboard'; // �깃났 �� ���쒕낫�쒕줈 由щ떎�대젆�� + } + } catch (error) { + console.error('鍮꾨�踰덊샇 蹂�寃� �붿껌 以� �ㅻ쪟:', error); + alert('鍮꾨�踰덊샇 蹂�寃� 以� �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.'); + } + }); + </script> +</body> +</html> diff --git a/login/route/conf_name.js b/login/route/conf_name.js new file mode 100644 index 0000000000000000000000000000000000000000..06c88e00fbfb8003d6557c9017d4a41d0cf4b806 --- /dev/null +++ b/login/route/conf_name.js @@ -0,0 +1,36 @@ +const express = require('express'); +const User = require('../models/user'); +const bcrypt = require('bcrypt'); +const path = require('path'); // 寃쎈줈 泥섎━ 紐⑤뱢 異붽� + +const router = express.Router(); +router.get('/change', (req, res) => { + + // �됰꽕�� 蹂�寃� HTML �뚯씪 諛섑솚 + res.sendFile(path.join(__dirname, '../public/conf_name.html')); // HTML �뚯씪 寃쎈줈 �ㅼ젙 +}); + +//�ъ슜�� �대쫫 蹂�寃�(�됰꽕��) +router.post('/change', async (req, res) => { + const { name, newName } = req.body; + + const user = await User.findOne({ name }); + + try { + if (!name || !newName) { + return res.status(400).json({message: '�좏슚�섏� �딆� �붿껌�낅땲��.'}); + } + + + user.name = newName; + await user.save(); + + res.status(200).json({ message: ' �대쫫�� �깃났�곸쑝濡� 蹂�寃쎈릺�덉뒿�덈떎.', user}); + console.log('�대쫫 蹂�寃� �꾨즺', user); + } catch(error) { + console.error('�대쫫 蹂�寃� �ㅻ쪟', error); + res.status(500).json({ message: '�쒕쾭 �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.'}); + } +}); + +module.exports = router; \ No newline at end of file diff --git a/login/route/conf_password.js b/login/route/conf_password.js new file mode 100644 index 0000000000000000000000000000000000000000..9d91354138cb869d43b583c0124f69f7c8414948 --- /dev/null +++ b/login/route/conf_password.js @@ -0,0 +1,40 @@ +const express = require('express'); +const bcrypt = require('bcrypt'); +const User = require('../models/user'); // User 紐⑤뜽 媛��몄삤湲� +const path = require('path'); // 寃쎈줈 泥섎━ 紐⑤뱢 異붽� + +const router = express.Router(); + +router.get('/change', (req, res) => { + + // 鍮꾨�踰덊샇 蹂�寃� HTML �뚯씪 諛섑솚 + res.sendFile(path.join(__dirname, '../public/conf_password.html')); // HTML �뚯씪 寃쎈줈 �ㅼ젙 +}); +// 鍮꾨�踰덊샇 蹂�寃� 濡쒖쭅 +router.post('/change', async (req, res) => { + const { email, oldPassword, newPassword } = req.body; + + try { + const user = await User.findOne({ email }); + if (!user || !user.password) { + return res.status(400).json({ message: '�ъ슜�먮� 李얠쓣 �� �녾굅�� �섎せ�� �붿껌�낅땲��.' }); + } + + const isMatch = await bcrypt.compare(oldPassword, user.password); + if (!isMatch) { + return res.status(400).json({ message: '湲곗〈 鍮꾨�踰덊샇媛� �쇱튂�섏� �딆뒿�덈떎.' }); + } + + const hashedPassword = await bcrypt.hash(newPassword, 10); + user.password = hashedPassword; + + await user.save(); + res.status(200).json({ message: '鍮꾨�踰덊샇媛� �깃났�곸쑝濡� 蹂�寃쎈릺�덉뒿�덈떎.' }); + console.log('鍮꾨�踰덊샇 蹂�寃� �꾨즺',user); + } catch (error) { + console.error('鍮꾨�踰덊샇 蹂�寃� �ㅻ쪟:', error); + res.status(500).json({ message: '�쒕쾭 �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.' }); + } +}); + +module.exports = router;