diff --git a/server/server.js b/server/server.js index 4e3160d..0aeae58 100644 --- a/server/server.js +++ b/server/server.js @@ -6,6 +6,9 @@ import express from "express"; import cors from 'cors' import bodyParser from "body-parser" import coursesRouter from './api/courses.js'; +import AWS from "aws-sdk"; // Use import for aws-sdk + + // CONSTANTS @@ -13,6 +16,21 @@ const PORT = process.env.PORT || 5000; const app = express(); +// AWS CONFIG +let awsConfig = { + "region": "us-west-2", + "accessKeyId": process.env.AWS_ACCESS_KEY, + "secretAccessKey": process.env.AWS_SECRET_KEY +}; + +AWS.config.update(awsConfig); + +// Create a new SES object +const ses = new AWS.SES({ apiVersion: "2010-12-01" }); + +// Create a new CognitoIdentityServiceProvider object +const cognito = new AWS.CognitoIdentityServiceProvider(); + // MIDDLEWARE app.use(cors()) app.use(bodyParser.json()) @@ -30,6 +48,94 @@ app.get('/', async (req, res) => { }); +// signup route +app.post('/signup', async (req, res) => { + const { password, email } = req.body; + + + + const params = { + ClientId: process.env.COGNITO_CLIENT_ID, // Your Cognito app client ID + Username: email, + Password: password, + UserAttributes: [ + { Name: 'email', Value: email } + + ] + }; + + try { + // Sign up the user + const data = await cognito.signUp(params).promise(); + + console.log('User signed up successfully:', data); + const userId = data.UserSub; + + res.status(200).json({ success: true, message: 'User signed up successfully', userId}); + } catch (error) { + console.error('Error signing up user:', error); + res.status(500).json({ success: false, error: 'Error signing up user: ' + error.message }); + } + }); + +// verification of email route + +app.post('/verify', async (req, res) => { + const { email, verificationCode } = req.body; + + const params = { + ClientId: process.env.COGNITO_CLIENT_ID, // Your Cognito app client ID + Username: email, + ConfirmationCode: verificationCode + }; + + try { + // Confirm user's email address + await cognito.confirmSignUp(params).promise(); + + console.log('User email confirmed successfully'); + + res.status(200).json({ success: true, message: 'Email confirmed successfully. You can now log in.' }); + + + } catch (error) { + console.error('Error confirming email:', error); + res.status(500).json({ success: false, error: 'Internal Server Error' }); + } +}); + + +app.post('/login', async (req, res) => { + const { email, password } = req.body; + + const params = { + AuthFlow: 'USER_PASSWORD_AUTH', + ClientId: process.env.COGNITO_CLIENT_ID, + AuthParameters: { + USERNAME: email, + PASSWORD: password, + }, + }; + + try { + const data = await cognito.initiateAuth(params).promise(); + console.log("Login successful:", data); + // Extract the user ID from the ID token + const token = data.AuthenticationResult.IdToken; + const decodedToken = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()); + const userId = decodedToken.sub; + res.status(200).json({ + success: true, + message: "Login successful", + userId, // Return the unique user ID + }); + } catch (error) { + console.error("Error during login:", error); + res.status(400).json({ success: false, message: error.message }); + } +}); + + // LISTEN app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`);