v1.0.0 #28
|
@ -27,6 +27,7 @@ services:
|
|||
MONGODB_URI: "mongodb://mongodb:27017/bot"
|
||||
MONGODB_USER: "root"
|
||||
MONGODB_PASS: "password"
|
||||
JWTSECRET: "cooljwtsecret"
|
||||
volumes:
|
||||
- ./:/usr/src/app:ro
|
||||
|
||||
|
|
|
@ -1,24 +1,30 @@
|
|||
db.createUser({
|
||||
user: 'root',
|
||||
pwd: 'password',
|
||||
roles: [
|
||||
{
|
||||
role: 'readWrite',
|
||||
db: 'admin',
|
||||
},
|
||||
{
|
||||
role: 'readWrite',
|
||||
db: 'bot',
|
||||
},
|
||||
],
|
||||
user: "root",
|
||||
pwd: "password",
|
||||
roles: [
|
||||
{
|
||||
role: "readWrite",
|
||||
db: "admin",
|
||||
},
|
||||
{
|
||||
role: "readWrite",
|
||||
db: "bot",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
db = new Mongo().getDB("bot");
|
||||
|
||||
db.images.createIndex({ "status": 1 });
|
||||
db.images.createIndex({ "url": 1 }, { "unique": true });
|
||||
db.images.createIndex({ status: 1 });
|
||||
db.images.createIndex({ url: 1 }, { unique: true });
|
||||
db.images.insert({
|
||||
url: "https://example.com",
|
||||
status: "consumed",
|
||||
tags: ["2girls", "sleeping"]
|
||||
});
|
||||
url: "https://example.com",
|
||||
status: "consumed",
|
||||
tags: ["2girls", "sleeping"],
|
||||
});
|
||||
|
||||
db.authorizations.createIndex({ app: 1 });
|
||||
db.authorizations.insert({
|
||||
app: "tester",
|
||||
secret: "test",
|
||||
});
|
||||
|
|
|
@ -2,9 +2,9 @@ import jwt from "jsonwebtoken";
|
|||
import AuthService from "../services/AuthService";
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
|
||||
class AuthControler {
|
||||
authTokenSecret = process.env.JWTSECRET || "badsecret";
|
||||
const authTokenSecret = process.env.JWTSECRET || "badsecret";
|
||||
|
||||
class AuthControler {
|
||||
async login(req: Request, res: Response) {
|
||||
// Read app and secret from request body
|
||||
const { app, secret } = req.body;
|
||||
|
@ -17,12 +17,12 @@ class AuthControler {
|
|||
// Generate an access token
|
||||
const accessToken = jwt.sign(
|
||||
{ app: authenticated.app },
|
||||
this.authTokenSecret,
|
||||
authTokenSecret,
|
||||
{ expiresIn: "1h" }
|
||||
);
|
||||
|
||||
res.json({
|
||||
accessToken,
|
||||
token: accessToken,
|
||||
});
|
||||
} else {
|
||||
res.status(403).send("Credentials incorrect");
|
||||
|
@ -34,7 +34,7 @@ class AuthControler {
|
|||
if (authHeader) {
|
||||
const token = authHeader.split(" ")[1];
|
||||
|
||||
jwt.verify(token, this.authTokenSecret, (err, app) => {
|
||||
jwt.verify(token, authTokenSecret, (err, app) => {
|
||||
if (err) {
|
||||
return res.status(403).json("Invalid token provided");
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import express from "express";
|
|||
import mongoose from "mongoose";
|
||||
import listEndpoints from "express-list-endpoints";
|
||||
import imageController from "./controllers/ImageController";
|
||||
import authControler from "./controllers/AuthControler";
|
||||
|
||||
export const app = express();
|
||||
|
||||
|
@ -14,6 +15,7 @@ app.get("/", (_, res) => {
|
|||
|
||||
app.get("/images", imageController.getAllImages);
|
||||
app.post("/images", imageController.addImage);
|
||||
app.post("/login", authControler.login)
|
||||
|
||||
// Set the default port to 8080, or use the PORT environment variable
|
||||
|
||||
|
|
|
@ -16,4 +16,4 @@ const AuthSchema = new mongoose.Schema({
|
|||
},
|
||||
});
|
||||
|
||||
export default mongoose.model("auth", AuthSchema);
|
||||
export default mongoose.model("authorizations", AuthSchema);
|
||||
|
|
|
@ -2,7 +2,8 @@ import AuthModel, { Auth } from "../models/AuthModel";
|
|||
|
||||
class AuthService {
|
||||
async find(app: String, secret: String): Promise<Auth | null> {
|
||||
return await AuthModel.findOne(app, secret);
|
||||
const auth = await AuthModel.findOne({ app: app, secret: secret }).exec();
|
||||
return auth;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue