diff --git a/src/Api/.env b/src/Api/.env deleted file mode 100644 index c88bfa8..0000000 --- a/src/Api/.env +++ /dev/null @@ -1 +0,0 @@ -PORT=8080 \ No newline at end of file diff --git a/src/Api/dist/app.js b/src/Api/dist/app.js new file mode 100644 index 0000000..390f118 --- /dev/null +++ b/src/Api/dist/app.js @@ -0,0 +1,48 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const express_1 = __importDefault(require("express")); +const cors_1 = __importDefault(require("cors")); +const config_1 = require("./config"); +const cookie_parser_1 = __importDefault(require("cookie-parser")); +const body_parser_1 = __importDefault(require("body-parser")); +const mongoose_1 = __importDefault(require("mongoose")); +class App { + constructor(controllers, port) { + this.express = (0, express_1.default)(); + this.port = port; + this.initDatabase(); + this.initMiddleware(); + this.initControllers(controllers); + } + initMiddleware() { + this.express.use((0, cors_1.default)()); + this.express.use((0, cookie_parser_1.default)()); + this.express.use(express_1.default.json()); + this.express.use(express_1.default.urlencoded({ extended: false })); + this.express.use(body_parser_1.default.json()); + this.express.use(body_parser_1.default.urlencoded({ + extended: true + })); + } + initControllers(controllers) { + controllers.forEach((controller) => { + this.express.use('/api', controller.router); + }); + } + listen() { + this.express.listen(this.port, () => { + console.log(`[server] : App listening on the port ${this.port}`); + }); + } + initDatabase() { + const MONGO_URL = `mongodb+srv://FladDev:${config_1.MONGO_PASSWORD}@flad.mliekr2.mongodb.net/?retryWrites=true&w=majority`; + mongoose_1.default.connect(MONGO_URL) + .then(() => console.log("Connect to MongoDB database successfully")) + .catch(error => console.log("Error connecting : " + error)); + } +} +exports.default = App; +//# sourceMappingURL=app.js.map \ No newline at end of file diff --git a/src/Api/dist/app.js.map b/src/Api/dist/app.js.map new file mode 100644 index 0000000..e9d8123 --- /dev/null +++ b/src/Api/dist/app.js.map @@ -0,0 +1 @@ +{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],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AG,EAAE;YAChC,OAAO,CAAC,GAAG,CAAC,wCAAwC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QACrE,CAAC,CAAC,CAAC;IACP,CAAC;IAEO,YAAY;QAChB,MAAM,SAAS,GAAG,yBAAyB,uBAAc,wDAAwD,CAAC;QAClH,kBAAQ,CAAC,OAAO,CAAC,SAAS,CAAC;aACtB,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,0CAA0C,CAAC,CAAC;aACnE,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,qBAAqB,GAAG,KAAK,CAAC,CAAC,CAAC;IACpE,CAAC;CACJ;AAED,kBAAe,GAAG,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/config.js b/src/Api/dist/config.js new file mode 100644 index 0000000..da704e7 --- /dev/null +++ b/src/Api/dist/config.js @@ -0,0 +1,7 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.CLIENT_SECRET_SPOTIFY = exports.CLIENT_ID_SPOTIFY = exports.MONGO_PASSWORD = void 0; +exports.MONGO_PASSWORD = "ZXN11dj7VC0RUYwa"; //process.env.MONGO; +exports.CLIENT_ID_SPOTIFY = "cd55847a89324a9a9af11aa6726bc1f9"; +exports.CLIENT_SECRET_SPOTIFY = "b49e81e3c3a643aa9ce1b2158219a2fb"; +//# sourceMappingURL=config.js.map \ No newline at end of file diff --git a/src/Api/dist/config.js.map b/src/Api/dist/config.js.map new file mode 100644 index 0000000..6a46717 --- /dev/null +++ b/src/Api/dist/config.js.map @@ -0,0 +1 @@ +{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;AAAa,QAAA,cAAc,GAAG,kBAAkB,CAAC,CAAC,oBAAoB;AACzD,QAAA,iBAAiB,GAAG,kCAAkC,CAAC;AACvD,QAAA,qBAAqB,GAAG,kCAAkC,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/controllers/interfaces/IController.js b/src/Api/dist/controllers/interfaces/IController.js new file mode 100644 index 0000000..2aca043 --- /dev/null +++ b/src/Api/dist/controllers/interfaces/IController.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=IController.js.map \ No newline at end of file diff --git a/src/Api/dist/controllers/interfaces/IController.js.map b/src/Api/dist/controllers/interfaces/IController.js.map new file mode 100644 index 0000000..60c7de6 --- /dev/null +++ b/src/Api/dist/controllers/interfaces/IController.js.map @@ -0,0 +1 @@ +{"version":3,"file":"IController.js","sourceRoot":"","sources":["../../../src/controllers/interfaces/IController.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/src/Api/dist/controllers/spotifyController.js b/src/Api/dist/controllers/spotifyController.js new file mode 100644 index 0000000..9fef5a2 --- /dev/null +++ b/src/Api/dist/controllers/spotifyController.js @@ -0,0 +1,131 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const express_1 = require("express"); +const config_1 = require("../config"); +const HttpException_1 = __importDefault(require("../exception/HttpException")); +const axios_1 = __importDefault(require("axios")); +const qs_1 = __importDefault(require("qs")); +class SpotifyController { + constructor() { + this.path = '/spotify'; + this.router = (0, express_1.Router)(); + this.API_URL = "https://accounts.spotify.com/api/token"; + this.CALLBACK = 'http://localhost:8080/api/spotify/callback'; + this.SCOPES = 'user-read-private user-read-email user-read-playback-state user-read-currently-playing user-read-recently-played playlist-modify-public ugc-image-upload user-modify-playback-state'; + this.clientRedirect = 'spotify_final_redirect-uri-key'; + this.login = (req, res, next) => __awaiter(this, void 0, void 0, function* () { + try { + const redirectResponse = req.query.redirectUrl ? req.query.redirectUrl : req.headers.referer; + res.cookie(this.clientRedirect, redirectResponse); + res.redirect('https://accounts.spotify.com/authorize?' + + qs_1.default.stringify({ + response_type: 'code', + client_id: config_1.CLIENT_ID_SPOTIFY, + scope: this.SCOPES, + redirect_uri: this.CALLBACK, + })); + } + catch (error) { + next(new HttpException_1.default(400, "Cannot connect: " + error.message)); + } + }); + this.getRefreshToken = (req, res, next) => __awaiter(this, void 0, void 0, function* () { + try { + const params = req.query.refresh_token; + if (!req.query.refresh_token) { + return res.json({ + "error": "Parameter refresh_token missing" + }); + } + let authOptions = { + method: 'POST', + url: 'https://accounts.spotify.com/api/token', + data: qs_1.default.stringify({ + grant_type: 'refresh_token', + refresh_token: params + }), + headers: { + 'Authorization': 'Basic ' + (Buffer.from(config_1.CLIENT_ID_SPOTIFY + ':' + config_1.CLIENT_SECRET_SPOTIFY).toString('base64')), + 'Content-Type': 'application/x-www-form-urlencoded' + }, + json: true + }; + (0, axios_1.default)(authOptions) + .then(session => { + if (session.status === 200) { + console.log('### Information : responce ###' + JSON.stringify(session.data)); + console.log('### Information : refresh_token ###' + session.data.refresh_token); + res.send({ + "access_token": session.data.access_token, + "refresh_token": session.data.refresh_token, + "expires_in": session.data.expires_in + }); + } + }); + console.log("goood"); + } + catch (error) { + console.log("errur"); + next(new HttpException_1.default(400, 'Cannot create post')); + } + }); + this.getAccessToken = (req, res, next) => __awaiter(this, void 0, void 0, function* () { + let code = req.query.code; + let storedredirectUri = req.cookies ? req.cookies[this.clientRedirect] : null; + var authOptions = { + method: 'POST', + url: this.API_URL, + data: qs_1.default.stringify({ + code: code, + redirect_uri: this.CALLBACK, + grant_type: 'authorization_code' + }), + headers: { + 'Authorization': 'Basic ' + (Buffer.from(config_1.CLIENT_ID_SPOTIFY + ':' + config_1.CLIENT_SECRET_SPOTIFY).toString('base64')), + 'Content-Type': 'application/x-www-form-urlencoded' + }, + json: true + }; + try { + var resp = yield (0, axios_1.default)(authOptions); + if (resp.status === 200) { + let access_token = resp.data.access_token; + let expiration = resp.data.expires_in; + let refresh = resp.data.refresh_token; + console.log(access_token); + res.clearCookie(this.clientRedirect); + res.redirect(`${storedredirectUri}?` + + qs_1.default.stringify({ + "access_token": access_token, + "expires_in": expiration, + "refresh_token": refresh + })); + } + } + catch (error) { + console.log(error); + next(new HttpException_1.default(400, 'Error connection: ' + error.message)); + } + }); + this.initRoutes(); + } + initRoutes() { + this.router.get(`${this.path}/exchange`, this.login); + this.router.get(`${this.path}/callback`, this.getAccessToken); + this.router.get(`${this.path}/refresh`, this.getRefreshToken); + } +} +exports.default = SpotifyController; +//# sourceMappingURL=spotifyController.js.map \ No newline at end of file diff --git a/src/Api/dist/controllers/spotifyController.js.map b/src/Api/dist/controllers/spotifyController.js.map new file mode 100644 index 0000000..9270d50 --- /dev/null +++ b/src/Api/dist/controllers/spotifyController.js.map @@ -0,0 +1 @@ +{"version":3,"file":"spotifyController.js","sourceRoot":"","sources":["../../src/controllers/spotifyController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AACA,qCAAkE;AAClE,sCAAqE;AACrE,+EAAuD;AACvD,kDAA0B;AAC1B,4CAAoB;AAEpB,MAAM,iBAAiB;IAOrB;QANO,SAAI,GAAG,UAAU,CAAC;QAClB,WAAM,GAAG,IAAA,gBAAM,GAAE,CAAC;QACR,YAAO,GAAG,wCAAwC,CAAC;QACnD,aAAQ,GAAG,4CAA4C,CAAC;QACxD,WAAM,GAAG,qLAAqL,CAAC;QAY/L,mBAAc,GAAG,gCAAgC,CAAC;QAE3D,UAAK,GAAG,CACd,GAAY,EACZ,GAAa,EACb,IAAkB,EACQ,EAAE;YAC5B,IAAI;gBACF,MAAM,gBAAgB,GAAG,GAAG,CAAC,KAAK,CAAC,WAAW,CA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AAG,CAAC,GAAG,IAAI,CAAC,IAAI,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;QACrD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;QAC9D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI,UAAU,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;IAChE,CAAC;CAkHF;AAED,kBAAe,iBAAiB,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/controllers/userController.js b/src/Api/dist/controllers/userController.js new file mode 100644 index 0000000..c1a06e7 --- /dev/null +++ b/src/Api/dist/controllers/userController.js @@ -0,0 +1,81 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const express_1 = require("express"); +const HttpException_1 = __importDefault(require("../exception/HttpException")); +const UserService_1 = __importDefault(require("../services/UserService")); +const UserValidation_1 = __importDefault(require("../middlewares/UserValidation")); +const validationMiddleware_1 = __importDefault(require("../middlewares/validationMiddleware")); +const authMiddleware_1 = __importDefault(require("../middlewares/authMiddleware")); +const LocationService_1 = __importDefault(require("../services/LocationService")); +class UserController { + constructor() { + this.path = '/users'; + this.router = (0, express_1.Router)(); + this.userService = new UserService_1.default(); + this.locationService = new LocationService_1.default(); + this.register = (req, res, next) => __awaiter(this, void 0, void 0, function* () { + try { + const { name, email, password, idSpotify } = req.body; + const token = yield this.userService.register(name, email, password, idSpotify); + res.status(201).json({ token }); + } + catch (error) { + next(new HttpException_1.default(400, error.message)); + } + }); + this.login = (req, res, next) => __awaiter(this, void 0, void 0, function* () { + try { + const { email, password } = req.body; + const token = yield this.userService.login(email, password); + res.status(200).json({ token }); + } + catch (error) { + next(new HttpException_1.default(400, error.message)); + } + }); + this.getUser = (req, res, next) => { + if (!req.user) { + return next(new HttpException_1.default(404, 'No logged in user')); + } + res.status(200).send({ data: req.user }); + }; + this.getUserNext = (req, res, next) => __awaiter(this, void 0, void 0, function* () { + try { + const longitude = Number(req.query.longitude); + const latitude = Number(req.query.latitude); + if (isNaN(longitude) || isNaN(latitude)) { + console.log('Unable to convert string to number'); + throw new Error('Unable to convert string to number'); + } + const userId = req.user.id; + const musicId = String(req.query.currentMusic); + const data = yield this.locationService.getNearUser(userId, musicId, latitude, longitude); + res.status(201).send(data); + } + catch (error) { + next(new HttpException_1.default(400, 'Cannot create get netUser: ' + error.message)); + } + }); + this.initRoutes(); + } + initRoutes() { + this.router.post(`${this.path}/register`, (0, validationMiddleware_1.default)(UserValidation_1.default.register), this.register); + this.router.post(`${this.path}/login`, (0, validationMiddleware_1.default)(UserValidation_1.default.login), this.login); + this.router.get(`${this.path}`, authMiddleware_1.default, this.getUser); + this.router.get(`${this.path}/nextTo`, authMiddleware_1.default, this.getUserNext); + } +} +exports.default = UserController; +//# sourceMappingURL=userController.js.map \ No newline at end of file diff --git a/src/Api/dist/controllers/userController.js.map b/src/Api/dist/controllers/userController.js.map new file mode 100644 index 0000000..1f0c776 --- /dev/null +++ b/src/Api/dist/controllers/userController.js.map @@ -0,0 +1 @@ +{"version":3,"file":"userController.js","sourceRoot":"","sources":["../../src/controllers/userController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,qCAAkF;AAElF,+EAAuD;AAEvD,0EAAkD;AAClD,mFAAqD;AACrD,+FAAuE;AACvE,mFAA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AC/E;QACL,CAAC,CAAA,CAAA;QAlFG,IAAI,CAAC,UAAU,EAAE,CAAC;IACtB,CAAC;IAEO,UAAU;QACd,IAAI,CAAC,MAAM,CAAC,IAAI,CACZ,GAAG,IAAI,CAAC,IAAI,WAAW,EACvB,IAAA,8BAAoB,EAAC,wBAAS,CAAC,QAAQ,CAAC,EACxC,IAAI,CAAC,QAAQ,CAChB,CAAC;QACF,IAAI,CAAC,MAAM,CAAC,IAAI,CACZ,GAAG,IAAI,CAAC,IAAI,QAAQ,EACpB,IAAA,8BAAoB,EAAC,wBAAS,CAAC,KAAK,CAAC,EACrC,IAAI,CAAC,KAAK,CACb,CAAC;QACF,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,EAAE,wBAAa,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC7D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI,SAAS,EAAE,wBAAa,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IAC5E,CAAC;CAmEJ;AAED,kBAAe,cAAc,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/database/LocationSchema.js b/src/Api/dist/database/LocationSchema.js new file mode 100644 index 0000000..c3da944 --- /dev/null +++ b/src/Api/dist/database/LocationSchema.js @@ -0,0 +1,24 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const mongoose_1 = require("mongoose"); +const locationSchema = new mongoose_1.Schema({ + userId: { + type: String, + required: true, + unique: true, + }, + musicId: { + type: String, + required: true, + }, + latitude: { + type: Number, + required: true, + }, + longitude: { + type: Number, + required: true, + } +}, { timestamps: true }); +exports.default = (0, mongoose_1.model)('Location', locationSchema); +//# sourceMappingURL=LocationSchema.js.map \ No newline at end of file diff --git a/src/Api/dist/database/LocationSchema.js.map b/src/Api/dist/database/LocationSchema.js.map new file mode 100644 index 0000000..ad24df8 --- /dev/null +++ b/src/Api/dist/database/LocationSchema.js.map @@ -0,0 +1 @@ +{"version":3,"file":"LocationSchema.js","sourceRoot":"","sources":["../../src/database/LocationSchema.ts"],"names":[],"mappings":";;AAAA,uCAAyC;AAGzC,MAAM,cAAc,GAAG,IAAI,iBAAM,CAAC;IAC9B,MAAM,EAAE;QACJ,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,IAAI;KACf;IACD,OAAO,EAAE;QACL,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;KACjB;IACD,QAAQ,EAAE;QACN,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;KACjB;IACD,SAAS,EAAE;QACP,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;KACjB;CACJ,EACG,EAAE,UAAU,EAAE,IAAI,EAAE,CACvB,CAAC;AAEF,kBAAe,IAAA,gBAAK,EAAW,UAAU,EAAE,cAAc,CAAC,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/database/UserSchema.js b/src/Api/dist/database/UserSchema.js new file mode 100644 index 0000000..beddd8a --- /dev/null +++ b/src/Api/dist/database/UserSchema.js @@ -0,0 +1,55 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const mongoose_1 = require("mongoose"); +const bcrypt_1 = __importDefault(require("bcrypt")); +const userSchema = new mongoose_1.Schema({ + idSpotify: { + type: String, + required: true, + unique: true + }, + name: { + type: String, + required: true, + unique: true + }, + email: { + type: String, + required: true, + unique: true, + trim: true + }, + password: { + type: String, + required: true + } +}, { timestamps: true }); +userSchema.pre('save', function (next) { + return __awaiter(this, void 0, void 0, function* () { + if (!this.isModified('password')) { + return next(); + } + const hash = yield bcrypt_1.default.hash(this.password, 8); + this.password = hash; + next(); + }); +}); +userSchema.methods.isValidPassword = function (password) { + return __awaiter(this, void 0, void 0, function* () { + return yield bcrypt_1.default.compare(password, this.password); + }); +}; +exports.default = (0, mongoose_1.model)('User', userSchema); +//# sourceMappingURL=UserSchema.js.map \ No newline at end of file diff --git a/src/Api/dist/database/UserSchema.js.map b/src/Api/dist/database/UserSchema.js.map new file mode 100644 index 0000000..855f973 --- /dev/null +++ b/src/Api/dist/database/UserSchema.js.map @@ -0,0 +1 @@ +{"version":3,"file":"UserSchema.js","sourceRoot":"","sources":["../../src/database/UserSchema.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AACA,uCAAyC;AACzC,oDAA4B;AAE5B,MAAM,UAAU,GAAG,IAAI,iBAAM,CAAC;IAC1B,SAAS,EAAE;QACP,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,IAAI;KACf;IACD,IAAI,EAAE;QACF,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,IAAI;KACf;IACD,KAAK,EAAE;QACH,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,IAAI;QACZ,IAAI,EAAE,IAAI;KACb;IACD,QAAQ,EAAE;QACN,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;KACjB;CACJ,EACG,EAAE,UAAU,EAAE,IAAI,EAAE,CACvB,CAAC;AAEF,UAAU,CAAC,GAAG,CAAO,MAAM,EAAE,UAAgB,IAAI;;QAC7C,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE;YAC9B,OAAO,IAAI,EAAE,CAAC;SACjB;QACD,MAAM,IAAI,GAAG,MAAM,gBAAM,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;QACjD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,IAAI,EAAE,CAAC;IACX,CAAC;CAAA,CAAC,CAAC;AAEH,UAAU,CAAC,OAAO,CAAC,eAAe,GAAG,UACjC,QAAgB;;QAEhB,OAAO,MAAM,gBAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IACzD,CAAC;CAAA,CAAC;AAEF,kBAAe,IAAA,gBAAK,EAAO,MAAM,EAAE,UAAU,CAAC,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/exception/HttpException.js b/src/Api/dist/exception/HttpException.js new file mode 100644 index 0000000..1145b8e --- /dev/null +++ b/src/Api/dist/exception/HttpException.js @@ -0,0 +1,11 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +class HttpException extends Error { + constructor(status, message) { + super(message); + this.status = status; + this.message = message; + } +} +exports.default = HttpException; +//# sourceMappingURL=HttpException.js.map \ No newline at end of file diff --git a/src/Api/dist/exception/HttpException.js.map b/src/Api/dist/exception/HttpException.js.map new file mode 100644 index 0000000..88818ab --- /dev/null +++ b/src/Api/dist/exception/HttpException.js.map @@ -0,0 +1 @@ +{"version":3,"file":"HttpException.js","sourceRoot":"","sources":["../../src/exception/HttpException.ts"],"names":[],"mappings":";;AAAA,MAAM,aAAc,SAAQ,KAAK;IAI7B,YAAY,MAAc,EAAE,OAAe;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;CACJ;AAED,kBAAe,aAAa,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/index.js b/src/Api/dist/index.js new file mode 100644 index 0000000..96edb20 --- /dev/null +++ b/src/Api/dist/index.js @@ -0,0 +1,14 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const app_1 = __importDefault(require("./app")); +const spotifyController_1 = __importDefault(require("./controllers/spotifyController")); +const userController_1 = __importDefault(require("./controllers/userController")); +const dotenv_1 = __importDefault(require("dotenv")); +dotenv_1.default.config(); +const app = new app_1.default([new spotifyController_1.default(), new userController_1.default()], Number(process.env.PORT)); +console.log(process.env.TMP); +app.listen(); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/src/Api/dist/index.js.map b/src/Api/dist/index.js.map new file mode 100644 index 0000000..60f96bb --- /dev/null +++ b/src/Api/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,gDAAwB;AACxB,wFAAgE;AAChE,kFAA0D;AAC1D,oDAA2B;AAE3B,gBAAM,CAAC,MAAM,EAAE,CAAC;AAChB,MAAM,GAAG,GAAG,IAAI,aAAG,CACf,CAAC,IAAI,2BAAiB,EAAE,EAAE,IAAI,wBAAc,EAAE,CAAC,EAC/C,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAC3B,CAAC;AAEF,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AAE5B,GAAG,CAAC,MAAM,EAAE,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/middlewares/UserValidation.js b/src/Api/dist/middlewares/UserValidation.js new file mode 100644 index 0000000..532ed65 --- /dev/null +++ b/src/Api/dist/middlewares/UserValidation.js @@ -0,0 +1,18 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const joi_1 = __importDefault(require("joi")); +const register = joi_1.default.object({ + name: joi_1.default.string().max(30).required(), + email: joi_1.default.string().email().required(), + password: joi_1.default.string().min(6).required(), + idSpotify: joi_1.default.string(), +}); +const login = joi_1.default.object({ + email: joi_1.default.string().email().required(), + password: joi_1.default.string().required(), +}); +exports.default = { register, login }; +//# sourceMappingURL=UserValidation.js.map \ No newline at end of file diff --git a/src/Api/dist/middlewares/UserValidation.js.map b/src/Api/dist/middlewares/UserValidation.js.map new file mode 100644 index 0000000..6a4adc2 --- /dev/null +++ b/src/Api/dist/middlewares/UserValidation.js.map @@ -0,0 +1 @@ +{"version":3,"file":"UserValidation.js","sourceRoot":"","sources":["../../src/middlewares/UserValidation.ts"],"names":[],"mappings":";;;;;AAAA,8CAAsB;AAEtB,MAAM,QAAQ,GAAG,aAAG,CAAC,MAAM,CAAC;IACxB,IAAI,EAAE,aAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,QAAQ,EAAE;IACrC,KAAK,EAAE,aAAG,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,QAAQ,EAAE;IACtC,QAAQ,EAAE,aAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE;IACxC,SAAS,EAAE,aAAG,CAAC,MAAM,EAAE;CAC1B,CAAC,CAAC;AAEH,MAAM,KAAK,GAAG,aAAG,CAAC,MAAM,CAAC;IACrB,KAAK,EAAE,aAAG,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,QAAQ,EAAE;IACtC,QAAQ,EAAE,aAAG,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CACpC,CAAC,CAAC;AAEH,kBAAe,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/middlewares/authMiddleware.js b/src/Api/dist/middlewares/authMiddleware.js new file mode 100644 index 0000000..be68e2d --- /dev/null +++ b/src/Api/dist/middlewares/authMiddleware.js @@ -0,0 +1,46 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); +const UserSchema_1 = __importDefault(require("../database/UserSchema")); +const TokenService_1 = __importDefault(require("../services/TokenService")); +const HttpException_1 = __importDefault(require("../exception/HttpException")); +function authMiddleware(req, res, next) { + return __awaiter(this, void 0, void 0, function* () { + const bearer = req.headers.authorization; + if (!bearer || !bearer.startsWith('Bearer ')) { + return next(new HttpException_1.default(401, 'Unauthorized')); + } + const accessToken = bearer.split('Bearer')[1].trim(); + try { + const payload = yield TokenService_1.default.verifyToken(accessToken); + if (payload instanceof jsonwebtoken_1.default.JsonWebTokenError) { + return next(new HttpException_1.default(401, 'Unauthorized')); + } + const user = yield UserSchema_1.default.findById(payload.id) + .select('-password') + .exec(); + if (!user) { + return next(new HttpException_1.default(401, 'Unauthorized')); + } + req.user = user; + return next(); + } + catch (error) { + return next(new HttpException_1.default(401, 'Unauthorized')); + } + }); +} +exports.default = authMiddleware; +//# sourceMappingURL=authMiddleware.js.map \ No newline at end of file diff --git a/src/Api/dist/middlewares/authMiddleware.js.map b/src/Api/dist/middlewares/authMiddleware.js.map new file mode 100644 index 0000000..df427be --- /dev/null +++ b/src/Api/dist/middlewares/authMiddleware.js.map @@ -0,0 +1 @@ +{"version":3,"file":"authMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/authMiddleware.ts"],"names":[],"mappings":";;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iddleware.js new file mode 100644 index 0000000..d17c507 --- /dev/null +++ b/src/Api/dist/middlewares/validationMiddleware.js @@ -0,0 +1,34 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +function validationMiddleware(schema) { + return (req, res, next) => __awaiter(this, void 0, void 0, function* () { + const validationOptions = { + abortEarly: false, + allowUnknown: true, + stripUnknown: true, + }; + try { + const value = yield schema.validateAsync(req.body, validationOptions); + req.body = value; + next(); + } + catch (e) { + const errors = []; + e.details.forEach((error) => { + errors.push(error.message); + }); + res.status(400).send({ errors: errors }); + } + }); +} +exports.default = validationMiddleware; +//# sourceMappingURL=validationMiddleware.js.map \ No newline at end of file diff --git a/src/Api/dist/middlewares/validationMiddleware.js.map b/src/Api/dist/middlewares/validationMiddleware.js.map new file mode 100644 index 0000000..3884754 --- /dev/null +++ b/src/Api/dist/middlewares/validationMiddleware.js.map @@ -0,0 +1 @@ +{"version":3,"file":"validationMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/validationMiddleware.ts"],"names":[],"mappings":";;;;;;;;;;;AAGA,SAAS,oBAAoB,CAAC,MAAkB;IAC5C,OAAO,CACH,GAAY,EACZ,GAAa,EACb,IAAkB,EACL,EAAE;QACf,MAAM,iBAAiB,GAAG;YACtB,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,IAAI;YAClB,YAAY,EAAE,IAAI;SACrB,CAAC;QAEF,IAAI;YACA,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,aAAa,CACpC,GAAG,CAAC,IAAI,EACR,iBAAiB,CACpB,CAAC;YACF,GAAG,CAAC,IAAI,GAAG,KAAK,CAAC;YACjB,IAAI,EAAE,CAAC;SACV;QAAC,OAAO,CAAM,EAAE;YACb,MAAM,MAAM,GAAa,EAAE,CAAC;YAC5B,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAA8B,EAAE,EAAE;gBACjD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC/B,CAAC,CAAC,CAAC;YACH,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;SAC5C;IACL,CAAC,CAAA,CAAC;AACN,CAAC;AAED,kBAAe,oBAAoB,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/models/Location.js b/src/Api/dist/models/Location.js new file mode 100644 index 0000000..491d7d8 --- /dev/null +++ b/src/Api/dist/models/Location.js @@ -0,0 +1,17 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Location = exports.UserLocation = void 0; +const mongoose_1 = require("mongoose"); +class UserLocation { + constructor(userId, musicId, latitude, longitude) { + this.userId = userId; + this.musicId = musicId; + this.latitude = latitude; + this.longitude = longitude; + } +} +exports.UserLocation = UserLocation; +class Location extends mongoose_1.Document { +} +exports.Location = Location; +//# sourceMappingURL=Location.js.map \ No newline at end of file diff --git a/src/Api/dist/models/Location.js.map b/src/Api/dist/models/Location.js.map new file mode 100644 index 0000000..d15af10 --- /dev/null +++ b/src/Api/dist/models/Location.js.map @@ -0,0 +1 @@ +{"version":3,"file":"Location.js","sourceRoot":"","sources":["../../src/models/Location.ts"],"names":[],"mappings":";;;AAAA,uCAAoC;AAEpC,MAAa,YAAY;IAKrB,YAAY,MAAc,EAAE,OAAgB,EAAC,QAAgB,EAAE,SAAiB;QAC5E,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC/B,CAAC;CACJ;AAXD,oCAWC;AAED,MAAa,QAAS,SAAQ,mBAAQ;CAKrC;AALD,4BAKC"} \ No newline at end of file diff --git a/src/Api/dist/models/Token.js b/src/Api/dist/models/Token.js new file mode 100644 index 0000000..2de6975 --- /dev/null +++ b/src/Api/dist/models/Token.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=Token.js.map \ No newline at end of file diff --git a/src/Api/dist/models/Token.js.map b/src/Api/dist/models/Token.js.map new file mode 100644 index 0000000..927818d --- /dev/null +++ b/src/Api/dist/models/Token.js.map @@ -0,0 +1 @@ +{"version":3,"file":"Token.js","sourceRoot":"","sources":["../../src/models/Token.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/src/Api/dist/models/User.js b/src/Api/dist/models/User.js new file mode 100644 index 0000000..9da5871 --- /dev/null +++ b/src/Api/dist/models/User.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=User.js.map \ No newline at end of file diff --git a/src/Api/dist/models/User.js.map b/src/Api/dist/models/User.js.map new file mode 100644 index 0000000..5598465 --- /dev/null +++ b/src/Api/dist/models/User.js.map @@ -0,0 +1 @@ +{"version":3,"file":"User.js","sourceRoot":"","sources":["../../src/models/User.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/src/Api/dist/services/LocationService.js b/src/Api/dist/services/LocationService.js new file mode 100644 index 0000000..04b3ca7 --- /dev/null +++ b/src/Api/dist/services/LocationService.js @@ -0,0 +1,65 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const Location_1 = require("../models/Location"); +const LocationSchema_1 = __importDefault(require("../database/LocationSchema")); +class LocationService { + constructor() { + this.locations = LocationSchema_1.default; + } + getNearUser(userId, musicId, latitude, longitude) { + return __awaiter(this, void 0, void 0, function* () { + yield this.locations.findOneAndUpdate({ userId }, { userId, musicId, latitude, longitude }, { upsert: true }); + const snapshot = yield this.locations.find({ userId: { $ne: userId } }); + if (!snapshot.length) { + console.log('No matching documents.'); + return; + } + let usersLocation = []; + snapshot.forEach(doc => { + usersLocation.push(new Location_1.UserLocation(doc.userId, doc.musicId, doc.latitude, doc.longitude)); + }); + const listUser = {}; + usersLocation.forEach(user => { + const distance = this.distanceBetween(latitude, longitude, user.latitude, user.longitude); + if (distance <= 100) { + listUser[user.userId] = user.musicId; + } + }); + return { listUser }; + }); + } + distanceBetween(lat1, lon1, lat2, lon2) { + if ((lat1 == lat2) && (lon1 == lon2)) { + return 0; + } + else { + var radlat1 = Math.PI * lat1 / 180; + var radlat2 = Math.PI * lat2 / 180; + var theta = lon1 - lon2; + var radtheta = Math.PI * theta / 180; + var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta); + if (dist > 1) { + dist = 1; + } + dist = Math.acos(dist); + dist = dist * 180 / Math.PI; + dist = dist * 60 * 1.1515; + dist = dist * 1.609344; + return dist; + } + } +} +exports.default = LocationService; +//# sourceMappingURL=LocationService.js.map \ No newline at end of file diff --git a/src/Api/dist/services/LocationService.js.map b/src/Api/dist/services/LocationService.js.map new file mode 100644 index 0000000..8274b0f --- /dev/null +++ b/src/Api/dist/services/LocationService.js.map @@ -0,0 +1 @@ +{"version":3,"file":"LocationService.js","sourceRoot":"","sources":["../../src/services/LocationService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iDAAkD;AAClD,gFAAwD;AAExD,MAAM,eAAe;IAArB;QACY,cAAS,GAAG,wBAAc,CAAC;IAmDvC,CAAC;IAlDgB,WAAW,CAAC,MAAc,EAAE,OAAe,EAAE,QAAgB,EAAE,SAAiB;;YACzF,MAAM,IAAI,CAAC,SAAS,CAAC,gBAAgB,CACjC,EAAE,MAAM,EAAE,EACV,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,EACxC,EAAE,MAAM,EAAE,IAAI,EAAE,CACnB,CAAC;YAEF,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;YACxE,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE;gBAClB,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;gBACtC,OAAO;aACV;YAED,IAAI,aAAa,GAAmB,EAAE,CAAC;YACvC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;gBACnB,aAAa,CAAC,IAAI,CAAC,IAAI,uBAAY,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;YAC/F,CAAC,CAAC,CAAC;YACH,MAAM,QAAQ,GAA2B,EAAE,CAAC;YAC5C,aAAa,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;gBACzB,MAAM,QAAQ,GAAG,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,SAAS,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;gBAC1F,IAAI,QAAQ,IAAI,GA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ff --git a/src/Api/dist/services/TokenService.js b/src/Api/dist/services/TokenService.js new file mode 100644 index 0000000..accf70a --- /dev/null +++ b/src/Api/dist/services/TokenService.js @@ -0,0 +1,34 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.verifyToken = exports.createToken = void 0; +const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); +const createToken = (user) => { + return jsonwebtoken_1.default.sign({ id: user._id }, "dave", { + expiresIn: '1d', + }); +}; +exports.createToken = createToken; +const verifyToken = (token) => __awaiter(void 0, void 0, void 0, function* () { + return new Promise((resolve, reject) => { + jsonwebtoken_1.default.verify(token, "dave", (err, payload) => { + if (err) + return reject(err); + resolve(payload); + }); + }); +}); +exports.verifyToken = verifyToken; +exports.default = { createToken: exports.createToken, verifyToken: exports.verifyToken }; +//# sourceMappingURL=TokenService.js.map \ No newline at end of file diff --git a/src/Api/dist/services/TokenService.js.map b/src/Api/dist/services/TokenService.js.map new file mode 100644 index 0000000..da9031a --- /dev/null +++ b/src/Api/dist/services/TokenService.js.map @@ -0,0 +1 @@ +{"version":3,"file":"TokenService.js","sourceRoot":"","sources":["../../src/services/TokenService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,gEAA+B;AAIxB,MAAM,WAAW,GAAG,CAAC,IAAU,EAAU,EAAE;IAC9C,OAAO,sBAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,MAAoB,EAAE;QACpD,SAAS,EAAE,IAAI;KAClB,CAAC,CAAC;AACP,CAAC,CAAC;AAJW,QAAA,WAAW,eAItB;AAEK,MAAM,WAAW,GAAG,CACvB,KAAa,EACoB,EAAE;IACnC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACnC,sBAAG,CAAC,MAAM,CACN,KAAK,EACL,MAAoB,EACpB,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE;YACb,IAAI,GAAG;gBAAE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;YAC5B,OAAO,CAAC,OAAgB,CAAC,CAAC;QAC9B,CAAC,CACJ,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC,CAAA,CAAC;AAbW,QAAA,WAAW,eAatB;AAEF,kBAAe,EAAE,WAAW,EAAX,mBAAW,EAAE,WAAW,EAAX,mBAAW,EAAE,CAAC"} \ No newline at end of file diff --git a/src/Api/dist/services/UserService.js b/src/Api/dist/services/UserService.js new file mode 100644 index 0000000..d869cae --- /dev/null +++ b/src/Api/dist/services/UserService.js @@ -0,0 +1,53 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const UserSchema_1 = __importDefault(require("../database/UserSchema")); +const TokenService_1 = __importDefault(require("./TokenService")); +class UserService { + constructor() { + this.user = UserSchema_1.default; + } + register(name, email, password, idSpotify) { + return __awaiter(this, void 0, void 0, function* () { + try { + const user = yield this.user.create({ + name, + email, + password, + idSpotify + }); + return TokenService_1.default.createToken(user); + } + catch (error) { + throw new Error(error.message); + } + }); + } + login(email, password) { + return __awaiter(this, void 0, void 0, function* () { + const user = yield this.user.findOne({ email }); + if (!user) { + throw new Error('Wrong credentials given'); + } + if (yield user.isValidPassword(password)) { + return TokenService_1.default.createToken(user); + } + else { + throw new Error('Wrong credentials given'); + } + }); + } +} +exports.default = UserService; +//# sourceMappingURL=UserService.js.map \ No newline at end of file diff --git a/src/Api/dist/services/UserService.js.map b/src/Api/dist/services/UserService.js.map new file mode 100644 index 0000000..27a8177 --- /dev/null +++ b/src/Api/dist/services/UserService.js.map @@ -0,0 +1 @@ +{"version":3,"file":"UserService.js","sourceRoot":"","sources":["../../src/services/UserService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,wEAAgD;AAChD,kEAAmC;AAEnC,MAAM,WAAW;IAAjB;QACY,SAAI,GAAG,oBAAU,CAAC;IAmC9B,CAAC;IAjCgB,QAAQ,CACjB,IAAY,EACZ,KAAa,EACb,QAAgB,EAChB,SAAiB;;YAEjB,IAAI;gBACA,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;oBAChC,IAAI;oBACJ,KAAK;oBACL,QAAQ;oBACR,SAAS;iBACZ,CAAC,CAAC;gBACH,OAAO,sBAAK,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;aAClC;YAAC,OAAO,KAAU,EAAE;gBACjB,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;aAClC;QACL,CAAC;KAAA;IAEY,KAAK,CACd,KAAa,EACb,QAAgB;;YAEhB,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;YAChD,IAAI,CAAC,IAAI,EAAE;gBACP,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;aAC9C;YACD,IAAI,MAAM,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,EAAE;gBACtC,OAAO,sBAAK,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;aAClC;iBAAM;gBACH,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;aAC9C;QACL,CAAC;KAAA;CACJ;AAED,kBAAe,WAAW,CAAC"} \ No newline at end of file diff --git a/src/Api/src/controllers/spotifyController.ts b/src/Api/src/controllers/spotifyController.ts index f3e09a1..29180ff 100644 --- a/src/Api/src/controllers/spotifyController.ts +++ b/src/Api/src/controllers/spotifyController.ts @@ -1,196 +1,139 @@ import IController from './interfaces/IController'; import { Router, Request, Response, NextFunction } from 'express'; +import { CLIENT_ID_SPOTIFY, CLIENT_SECRET_SPOTIFY } from '../config'; import HttpException from '../exception/HttpException'; import axios from 'axios'; import qs from 'qs'; class SpotifyController implements IController { - public path = '/spotify'; - public router = Router(); - - constructor() { - this.initialiseRoutes(); - } - - initialiseRoutes() { - this.router.get(`${this.path}/exchange`,this.login); - this.router.get(`${this.path}/callback`,this.getAccessToken); - this.router.get(`${this.path}/refresh`,this.getRefreshToken); - this.router.get(`${this.path}/spot`, this.getSpot); - } + public path = '/spotify'; + public router = Router(); + private readonly API_URL = "https://accounts.spotify.com/api/token"; + private readonly CALLBACK = 'http://localhost:8080/api/spotify/callback'; + private readonly SCOPES = 'user-read-private user-read-email user-read-playback-state user-read-currently-playing user-read-recently-played playlist-modify-public ugc-image-upload user-modify-playback-state'; - - private readonly API_URL = "https://accounts.spotify.com/api/token"; - private readonly CLIENT_ID = "1f1e34e4b6ba48b388469dba80202b10"; - private readonly CLIENT_SECRET = "779371c6d4994a68b8dd6e84b0873c82"; - private readonly CALLBACK_2 = 'https://flad-api-production.up.railway.app/api/spotify/callback'; - private readonly SCOPES ='user-read-private user-read-email user-read-playback-state user-read-currently-playing user-read-recently-played playlist-modify-public ugc-image-upload user-modify-playback-state'; - private readonly clientRedirect= 'spotify_final_redirect-uri-key'; + constructor() { + this.initRoutes(); + } - private login = async ( - req: Request, - res: Response, - next: NextFunction - ): Promise => { - - console.log("useeeee== login"); - try { - - const redirectResponse = req.query.redirectUrl ? req.query.redirectUrl : req.headers.referer; - res.cookie(this.clientRedirect, redirectResponse); - console.log("aloorrr si c'est niquuuuuuuuuuuueeee" +this.CALLBACK_2+ "gennnnnnnnnrree vraiiiiiiiment "); - res.redirect('https://accounts.spotify.com/authorize?' + - qs.stringify({ - response_type: 'code', - client_id: this.CLIENT_ID, - scope: this.SCOPES, - redirect_uri: this.CALLBACK_2, - })); - - - } catch (error) { - next(new HttpException(400, 'Cannot create spot')); - } - - - }; + initRoutes() { + this.router.get(`${this.path}/exchange`, this.login); + this.router.get(`${this.path}/callback`, this.getAccessToken); + this.router.get(`${this.path}/refresh`, this.getRefreshToken); + } - private getRefreshToken = async ( - req: Request, - res: Response, - next: NextFunction - ): Promise => { - - console.log('UUse2'); + private readonly clientRedirect = 'spotify_final_redirect-uri-key'; - try { - const params = req.query.refresh_token; - - if (!req.query.refresh_token) { - return res.json({ - "error": "Parameter refresh_token missing" - }); - } - let authOptions = { - method: 'POST', - url: 'https://accounts.spotify.com/api/token', - data: qs.stringify({ - grant_type: 'refresh_token', - refresh_token: params - }), - headers: { - 'Authorization': 'Basic ' + ( Buffer.from(this.CLIENT_ID + ':' + this.CLIENT_SECRET).toString('base64')), - 'Content-Type' : 'application/x-www-form-urlencoded' - }, - json: true - }; - - axios(authOptions) - .then(session => { - if(session.status === 200){ - console.log('### Information : responce ###' + JSON.stringify( session.data) ); - console.log('### Information : refresh_token ###' + session.data.refresh_token); - - res.send({ - "access_token": session.data.access_token, - "refresh_token": session.data.refresh_token, - "expires_in": session.data.expires_in - }); - }}); - console.log("goood"); - } catch (error) { - console.log("errur"); - next(new HttpException(400, 'Cannot create post')); - } - - } - - + private login = async ( + req: Request, + res: Response, + next: NextFunction + ): Promise => { + try { + const redirectResponse = req.query.redirectUrl ? req.query.redirectUrl : req.headers.referer; + res.cookie(this.clientRedirect, redirectResponse); + res.redirect('https://accounts.spotify.com/authorize?' + + qs.stringify({ + response_type: 'code', + client_id: CLIENT_ID_SPOTIFY, + scope: this.SCOPES, + redirect_uri: this.CALLBACK, + })); + } catch (error) { + next(new HttpException(400, "Cannot connect: " + error.message)); + } + }; - public getSpot = async ( - req: Request, - res: Response, - next: NextFunction + private getRefreshToken = async ( + req: Request, + res: Response, + next: NextFunction ): Promise => { - const spots = [ - { - name: "blue", - sourceUrl: "https://cdns-images.dzcdn.net/images/artist/399e7e760d8fedf3cc2891e9c0c41658/200x200-000000-80-0-0.jpg", - index: 3 - }, - { - name: "strange history", - sourceUrl: "https://images.genius.com/339dfe2a7c0adf9a5d08febf29a845f4.1000x1000x1.jpg", - index: 7 - }, - { - name: "oboy album", - sourceUrl: "https://i.pinimg.com/originals/ad/cc/d5/adccd58a0d0ff516a6114703cd05810e.jpg", - index: 1 - } - ]; try { - res.send(spots); - + const params = req.query.refresh_token; + if (!req.query.refresh_token) { + return res.json({ + "error": "Parameter refresh_token missing" + }); + } + let authOptions = { + method: 'POST', + url: 'https://accounts.spotify.com/api/token', + data: qs.stringify({ + grant_type: 'refresh_token', + refresh_token: params + }), + headers: { + 'Authorization': 'Basic ' + (Buffer.from(CLIENT_ID_SPOTIFY + ':' + CLIENT_SECRET_SPOTIFY).toString('base64')), + 'Content-Type': 'application/x-www-form-urlencoded' + }, + json: true + }; + + axios(authOptions) + .then(session => { + if (session.status === 200) { + console.log('### Information : responce ###' + JSON.stringify(session.data)); + console.log('### Information : refresh_token ###' + session.data.refresh_token); + + res.send({ + "access_token": session.data.access_token, + "refresh_token": session.data.refresh_token, + "expires_in": session.data.expires_in + }); + } + }); + console.log("goood"); } catch (error) { - console.log('heuuuuuuuuuuuuuuuuuuuuubizzzaaarrreeee'); - console.log(error); - next(new HttpException(400, 'On peut pas avoir darray mec')); - } } + console.log("errur"); + next(new HttpException(400, 'Cannot create post')); + } + } - private getAccessToken = async ( - req: Request, - res: Response, - next: NextFunction + private getAccessToken = async ( + req: Request, + res: Response, + next: NextFunction ): Promise => { - console.log("useeeee== accesToken"); - - let code = req.query.code; - let state = req.query.state || null; + let code = req.query.code; let storedredirectUri = req.cookies ? req.cookies[this.clientRedirect] : null; - + var authOptions = { method: 'POST', - url: 'https://accounts.spotify.com/api/token', + url: this.API_URL, data: qs.stringify({ code: code, - redirect_uri: this.CALLBACK_2, + redirect_uri: this.CALLBACK, grant_type: 'authorization_code' }), headers: { - 'Authorization': 'Basic ' + ( Buffer.from(this.CLIENT_ID + ':' + this.CLIENT_SECRET).toString('base64')), - 'Content-Type' : 'application/x-www-form-urlencoded' + 'Authorization': 'Basic ' + (Buffer.from(CLIENT_ID_SPOTIFY + ':' + CLIENT_SECRET_SPOTIFY).toString('base64')), + 'Content-Type': 'application/x-www-form-urlencoded' }, json: true }; try { - var resp = await axios(authOptions); - if (resp.status === 200) { - console.log('oon esttt laaa'); - let access_token = resp.data.access_token; - let expiration =resp.data.expires_in; - let refresh = resp.data.refresh_token - console.log(access_token); - - res.clearCookie(this.clientRedirect); - res.redirect(`${storedredirectUri}?` + - qs.stringify({ - "access_token": access_token, - "expires_in": expiration, - "refresh_token" : refresh - })); - } + var resp = await axios(authOptions); + if (resp.status === 200) { + let access_token = resp.data.access_token; + let expiration = resp.data.expires_in; + let refresh = resp.data.refresh_token + console.log(access_token); + + res.clearCookie(this.clientRedirect); + res.redirect(`${storedredirectUri}?` + + qs.stringify({ + "access_token": access_token, + "expires_in": expiration, + "refresh_token": refresh + })); + } } catch (error) { - console.log('heuuuuuuuuuuuuuuuuuuuuubizzzaaarrreeee'); console.log(error); - next(new HttpException(400, 'On peut pas te connecter mec'+ error.message)); + next(new HttpException(400, 'Error connection: ' + error.message)); } - - - }; - - } -export default SpotifyController; + +export default SpotifyController; \ No newline at end of file