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

Staging #63

Merged
merged 4 commits into from
Dec 3, 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
203 changes: 107 additions & 96 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "waifu.it",
"version": "4.7.0",
"version": "4.8.0",
"description": "Random API Serving Anime stuff",
"author": "Aeryk",
"private": true,
Expand Down
144 changes: 92 additions & 52 deletions src/controllers/v4/internal/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,81 +61,121 @@
};

/**
* Handles user-related operations based on the HTTP method.
* Processes user session by creating a new user if one doesn't exist,
* updating tokens if applicable, and handling authentication.
*
* @param {Object} req - Express request object.
* @param {Object} res - Express response object.
* @param {Function} next - Express next middleware function.
* @param {Object} req - Express request object containing headers and body.
* @param {Object} res - Express response object for sending responses.
* @param {Function} next - Express next middleware function for error handling.
* @returns {Object} - JSON response indicating success or failure.
*/
const userEndpoint = async (req, res, next) => {
const processUserSessionAndUpdate = async (req, res, next) => {
try {
const { body, headers, method } = req;
const { headers, body } = req;
const { token, id, email, 'access-token': access_token } = body;
const { key } = headers;

// Check for valid access key in headers
// Validate access key
if (!key || key !== process.env.ACCESS_KEY) {
return res.status(401).json({
message: 'Unauthorized',
});
return res.status(401).json({ message: 'Unauthorized' });
}

if (method === 'POST') {
const { token, id } = body;
// Validate User ID
if (!id) {
return res.status(400).json({ message: 'User ID is required' });
}

// Check for required fields in the request body
if (!token || !id) {
// Check if the user exists
const existingUser = await Users.findOne({ _id: id });
Dismissed Show dismissed Hide dismissed

if (!existingUser) {
// If the user doesn't exist, ensure required fields are provided
if (!email || !access_token) {
return res.status(400).json({
message: 'Token and User ID are required in the request body',
message: 'Email and access-token are required for new users',
});
}

// Update user's token in the database
await Users.updateOne(
{ _id: { $eq: id } },
{ $set: { token: token } },
{ upsert: true }, // Create the document if it doesn't exist
);

return res.status(200).json({
message: 'Token updated successfully',
// Create a new user with a generated token
const generatedToken = generateToken(id, process.env.HMAC_KEY);
const newUser = {
_id: id,
email,
token: generatedToken,
access_token,
password: crypto.randomBytes(22).toString('base64'), // Generate a random password
};

await Users.create(newUser);

return res.status(201).json({
message: 'User created successfully',
token: newUser.token,
});
} else if (method === 'GET') {
const { id, email } = headers;
} else {
// If the user exists, update the token if provided, and access-token if available
const updates = {};
if (token) updates.token = token;
if (access_token) updates.access_token = access_token;

// Check for required User ID in the headers
if (!id) {
return res.status(400).json({
message: 'User ID missing in the request body',
});
if (Object.keys(updates).length > 0) {
await Users.updateOne({ _id: id }, { $set: updates });
Dismissed Show dismissed Hide dismissed
}

// Fetch user details based on the provided user ID
const user = await Users.findOne({ _id: { $eq: id } });
if (token) {
return res.status(200).json({ message: 'Token updated successfully', token: token });
} else {
return res.status(200).json({ message: 'Logging successfully', token: existingUser.token });
}
}
} catch (error) {
console.error('Error in processUserSessionAndUpdate :', error.message);
return next(error);
}
};

if (!user) {
// If user not found, create a new user with the provided ID and token
const newUser = {
_id: id,
email: email,
password: crypto.randomBytes(22).toString('base64'),
token: generateToken(id, process.env.HMAC_KEY),
// Add other fields in the "newUser" object based on your schema
};
/**
* Fetches user data by ID, validates the access key, and updates the access token if provided.
*
* @param {Object} req - Express request object containing headers.
* @param {Object} res - Express response object for sending responses.
* @param {Function} next - Express next middleware function for error handling.
* @returns {Object} - JSON response with user token or error message.
*/
const getUser = async (req, res, next) => {
try {
const { headers } = req;
const { key } = headers;

await Users.create(newUser);
// Validate access key
if (!key || key !== process.env.ACCESS_KEY) {
return res.status(401).json({ message: 'Unauthorized' });
}

return res.status(201).json(newUser.token);
}
const { id, email, 'access-token': access_token } = headers;

return res.status(200).json(user.token);
} else {
return res.status(405).json({
message: 'Method Not Allowed',
});
// Validate User ID
if (!id) {
return res.status(400).json({ message: 'User ID is required' });
}

// Find user by ID
const user = await Users.findOne({ _id: id });

if (!user) {
return res.status(404).json({ message: 'User not found' });
}

// Update user's access token
if (access_token) {
await Users.updateOne({ _id: id }, { $set: { access_token } });
}

return res.status(200).json({ token: user.token });
} catch (error) {
return next(error);
console.error('Error in getUser:', error.message);
return res.status(500).json({ message: 'Internal Server Error' });
}
};

export { userEndpoint, retrieveUserProfile, updateUserToken };
export { retrieveUserProfile, updateUserToken, processUserSessionAndUpdate, getUser };
3 changes: 1 addition & 2 deletions src/models/schemas/Angry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const AngrySchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Angry', AngrySchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Baka.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const BakaSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Baka', BakaSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Bite.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const BiteSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Bite', BiteSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Blush.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const BlushSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Blush', BlushSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Bonk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const BonkSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Bonk', BonkSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Bored.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const BoredSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Bored', BoredSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Bully.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const BullySchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Bully', BullySchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Bye.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const ByeSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Bye', ByeSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Chase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const ChaseSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Chase', ChaseSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Cheer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const CheerSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Cheer', CheerSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Cringe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const CringeSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Cringe', CringeSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Cry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const CrySchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Cry', CrySchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Cuddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const CuddleSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Cuddle', CuddleSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Dab.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const DabSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Dab', DabSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Dance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const DanceSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Dance', DanceSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Die.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const DieSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Die', DieSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Disgust.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const DisgustSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Disgust', DisgustSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Facepalm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const FacepalmSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Facepalm', FacepalmSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const FeedSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Feed', FeedSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Glomp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const GlompSchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Glomp', GlompSchema);

3 changes: 1 addition & 2 deletions src/models/schemas/Happy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const { Schema, model } = mongoose;

const HappySchema = new Schema({
_id: { type: Number },
url: { type: String, required: true },
url: { type: String, required: true, unique: true },
});

export default model('Happy', HappySchema);

Loading
Loading