Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Authentication with mail notifications #10

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added server/controller/auth.js
Empty file.
30 changes: 30 additions & 0 deletions server/emailsend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
secure: true,
host: 'smtp.gmail.com',
port: 465,
auth: {
user: '[email protected]',
pass: 'pana spre gxji ozzp'
}
});

function sendEmail(email, sub, msg) {
// let to = email;
transporter.sendMail({
from: '[email protected]',
to: email,
subject: sub,
html:msg
}, (error, info) => {
if (error) {
console.log('Error occurred: ', error);
} else {
console.log('Email Sent: ', info.messageId);
}
});
}


module.exports = sendEmail;
125 changes: 85 additions & 40 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,115 @@ const cors = require('cors');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
require('dotenv').config(); // Load .env file
const sendEmail = require('./emailsend');
require('dotenv').config();

const app = express();
const PORT = process.env.PORT || 8080;


// Middleware setup
app.use(cors());
app.use(express.json());

// MongoDB connection string from environment variables
const mongoURI = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_CLUSTER}/${process.env.MONGO_DATABASE}?retryWrites=true&w=majority`;

mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected!'))
.catch(err => console.error('MongoDB connection error:', err));


const User = require('./models/User');
// MongoDB connection
mongoose.connect(mongoURI)
.then(() => console.log('MongoDB connected!'))
.catch(err => console.error('MongoDB connection error:', err));

// Email template for login notification
const loginEmailTemplate = `
<div style="max-width: 600px; margin: 50px auto; background-color: #ffffff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);">
<div style="text-align: center; background-color: #4CAF50; padding: 10px; border-radius: 8px 8px 0 0;">
<h1 style="color: white; margin: 0;">Welcome to Skin.AI</h1>
</div>
<div style="padding: 20px; color: #333333;">
<h2 style="color: #4CAF50;">Login Successful</h2>
<p>Hello,</p>
<p>Thank you for logging in to your Skin.AI account! We're happy to have you back.</p>
<p>If you have any questions, feel free to reply to this email. We're here to help!</p>
</div>
<div style="text-align: center; padding: 10px; background-color: #f4f4f4; border-radius: 0 0 8px 8px;">
<p style="font-size: 12px; color: #777777;">&copy; 2024 Skin.AI. All rights reserved.</p>
</div>
</div>
`;

const User = require('./models/User');
const JWT_SECRET = process.env.JWT_SECRET || 'your_jwt_secret';

// Register Route
app.post('/auth/register', async (req, res) => {
const { name, email, password, mobile, profileImage } = req.body;
try {
const { name, email, password, mobile, profileImage } = req.body;

if (!name || !email || !password || !mobile || !profileImage) {
return res.status(400).json({ message: 'All fields are required!' });
}
if (!name || !email || !password || !mobile || !profileImage) {
return res.status(400).json({ message: 'All fields are required!' });
}


const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: 'User already exists!' });
}
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: 'User already exists!' });
}

const hashedPassword = await bcrypt.hash(password, 10);
const newUser = new User({ name, email, password: hashedPassword, mobile, profileImage });
await newUser.save();


const hashedPassword = await bcrypt.hash(password, 10);


const newUser = new User({ name, email, password: hashedPassword, mobile, profileImage });
await newUser.save();
// Send thank-you email on registration
sendEmail(email, 'Thank You for Registering', '<h1>Thank you for registering at Skin.AI!</h1>');

return res.status(201).json({ message: 'User registered successfully', user: { name, email, mobile, profileImage } });
return res.status(201).json({ message: 'User registered successfully', user: { name, email, mobile, profileImage } });
} catch (error) {
console.error('Registration error:', error);
return res.status(500).json({ message: 'Server error, please try again later.' });
}
});

// Login Route
app.post('/auth/login', async (req, res) => {
const { email, password } = req.body;


const user = await User.findOne({ email });
if (!user) {
return res.status(400).json({ message: 'Invalid credentials!' });
}

const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(400).json({ message: 'Invalid credentials!' });
try {
const { email, password } = req.body;

const user = await User.findOne({ email });
if (!user) {
return res.status(400).json({ message: 'Invalid credentials!' });
}

const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(400).json({ message: 'Invalid credentials!' });
}

const token = jwt.sign({ email: user.email, name: user.name }, JWT_SECRET, { expiresIn: '1h' });

// Send thank-you email on login
sendEmail(email, 'Thank You for Logging In', <div style="max-width: 600px; margin: 50px auto; background-color: #ffffff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);">
<div style="text-align: center; background-color: #4CAF50; padding: 10px; border-radius: 8px 8px 0 0;">
<h1 style="color: white; margin: 0;">Welcome to Skin.AI</h1>
</div>
<div style="padding: 20px; color: #333333;">
<h2 style="color: #4CAF50;">Login Successful</h2>
<p>Hello,</p>
<p>Thank you for logging in to your Skin.AI account! We're happy to have you back.</p>
<p>If you have any questions, feel free to reply to this email. We're here to help!</p>
</div>
<div style="text-align: center; padding: 10px; background-color: #f4f4f4; border-radius: 0 0 8px 8px;">
<p style="font-size: 12px; color: #777777;">&copy; 2024 Skin.AI. All rights reserved.</p>
</div>
</div>
);

return res.json({ message: 'Logged in successfully', token });
} catch (error) {
console.error('Login error:', error);
return res.status(500).json({ message: 'Server error, please try again later.' });
}

const token = jwt.sign({ email: user.email, name: user.name }, JWT_SECRET, { expiresIn: '1h' });

return res.json({ message: 'Logged in successfully', token });
});


// Start the server
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});
1 change: 0 additions & 1 deletion server/models/User.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
Expand Down
Loading