|
|
@ -1,13 +1,9 @@
|
|
|
|
import Controller from '../Icontroller';
|
|
|
|
import Controller from '../Icontroller';
|
|
|
|
import { Router, Request, Response, NextFunction, RequestHandler } from 'express';
|
|
|
|
import { Router, Request, Response, NextFunction } from 'express';
|
|
|
|
import { AuthReqBody } from './request/authReqBody';
|
|
|
|
|
|
|
|
import HttpException from '../../middleware/exeption/httpExeption';
|
|
|
|
import HttpException from '../../middleware/exeption/httpExeption';
|
|
|
|
import axios from 'axios';
|
|
|
|
import axios from 'axios';
|
|
|
|
import CryptString from './crypt';
|
|
|
|
import CryptString from './crypt';
|
|
|
|
import AES from 'crypto-js'
|
|
|
|
|
|
|
|
import querystring from 'querystring';
|
|
|
|
|
|
|
|
import qs from 'qs';
|
|
|
|
import qs from 'qs';
|
|
|
|
import cookieParser from 'cookie-parser';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SpotifyController implements Controller {
|
|
|
|
class SpotifyController implements Controller {
|
|
|
|
public path = '/spotify';
|
|
|
|
public path = '/spotify';
|
|
|
@ -19,7 +15,6 @@ class SpotifyController implements Controller {
|
|
|
|
this.initialiseRoutes();
|
|
|
|
this.initialiseRoutes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
initialiseRoutes() {
|
|
|
|
initialiseRoutes() {
|
|
|
|
// this.router.post(`${this.path}`,this.createTask);
|
|
|
|
|
|
|
|
this.router.get(`${this.path}/exchange`,this.login);
|
|
|
|
this.router.get(`${this.path}/exchange`,this.login);
|
|
|
|
this.router.get(`${this.path}/callback`,this.getAccessToken);
|
|
|
|
this.router.get(`${this.path}/callback`,this.getAccessToken);
|
|
|
|
this.router.get(`${this.path}/refresh`,this.getRefreshToken);
|
|
|
|
this.router.get(`${this.path}/refresh`,this.getRefreshToken);
|
|
|
@ -27,15 +22,12 @@ class SpotifyController implements Controller {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// need to put in ENvironement file
|
|
|
|
|
|
|
|
// private readonly CLIENT_CALLBACK_URL = "http://localhost:8080/callback";
|
|
|
|
|
|
|
|
private readonly API_URL = "https://accounts.spotify.com/api/token";
|
|
|
|
private readonly API_URL = "https://accounts.spotify.com/api/token";
|
|
|
|
private readonly CLIENT_ID = "1f1e34e4b6ba48b388469dba80202b10";
|
|
|
|
private readonly CLIENT_ID = "1f1e34e4b6ba48b388469dba80202b10";
|
|
|
|
private readonly CLIENT_SECRET = "779371c6d4994a68b8dd6e84b0873c82";
|
|
|
|
private readonly CLIENT_SECRET = "779371c6d4994a68b8dd6e84b0873c82";
|
|
|
|
// private readonly CLIENT_CALLBACK_URL = "https://auth.expo.io/@thed47/FLAD//callback";
|
|
|
|
|
|
|
|
private readonly CALLBACK_2 = 'https://flad-api-production.up.railway.app/api/spotify/callback';
|
|
|
|
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 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 ENCRYPTION_SECRET = new CryptString(16);
|
|
|
|
|
|
|
|
private readonly clientRedirect= 'spotify_final_redirect-uri-key';
|
|
|
|
private readonly clientRedirect= 'spotify_final_redirect-uri-key';
|
|
|
|
|
|
|
|
|
|
|
|
private login = async (
|
|
|
|
private login = async (
|
|
|
@ -46,18 +38,7 @@ class SpotifyController implements Controller {
|
|
|
|
|
|
|
|
|
|
|
|
console.log("useeeee== login");
|
|
|
|
console.log("useeeee== login");
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// const params = req.body;
|
|
|
|
|
|
|
|
// if (!params.refresh_token) {
|
|
|
|
|
|
|
|
// return res.json({
|
|
|
|
|
|
|
|
// "error": "Parameter missing"
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this.spotifyRequest({
|
|
|
|
|
|
|
|
// grant_type: "authorization_code",
|
|
|
|
|
|
|
|
// redirect_uri: this.CLIENT_CALLBACK_2,
|
|
|
|
|
|
|
|
// // code: params.code
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
const redirectResponse = req.query.redirectUrl ? req.query.redirectUrl : req.headers.referer;
|
|
|
|
const redirectResponse = req.query.redirectUrl ? req.query.redirectUrl : req.headers.referer;
|
|
|
|
res.cookie(this.clientRedirect, redirectResponse);
|
|
|
|
res.cookie(this.clientRedirect, redirectResponse);
|
|
|
|
console.log("aloorrr si c'est niquuuuuuuuuuuueeee" +this.CALLBACK_2+ "gennnnnnnnnrree vraiiiiiiiment ");
|
|
|
|
console.log("aloorrr si c'est niquuuuuuuuuuuueeee" +this.CALLBACK_2+ "gennnnnnnnnrree vraiiiiiiiment ");
|
|
|
@ -67,27 +48,9 @@ class SpotifyController implements Controller {
|
|
|
|
client_id: this.CLIENT_ID,
|
|
|
|
client_id: this.CLIENT_ID,
|
|
|
|
scope: this.SCOPES,
|
|
|
|
scope: this.SCOPES,
|
|
|
|
redirect_uri: this.CALLBACK_2,
|
|
|
|
redirect_uri: this.CALLBACK_2,
|
|
|
|
// state: this.ENCRYPTION_SECRET.stringCrypt
|
|
|
|
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
// '?response_type=code' +
|
|
|
|
|
|
|
|
// '&client_id=' +
|
|
|
|
|
|
|
|
// "1f1e34e4b6ba48b388469dba80202b10" +
|
|
|
|
|
|
|
|
// (this.SCOPES ? '&scope=' + encodeURIComponent(this.SCOPES) : '') +
|
|
|
|
|
|
|
|
// '&redirect_uri=' +
|
|
|
|
|
|
|
|
// encodeURIComponent(this.CALLBACK_2)
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// .then(session => {
|
|
|
|
|
|
|
|
// let result = {
|
|
|
|
|
|
|
|
// "access_token": session.access_token,
|
|
|
|
|
|
|
|
// "expires_in": session.expires_in,
|
|
|
|
|
|
|
|
// "refresh_token": this.encrypt(session.refresh_token)
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// return res.send(result);
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
// .catch(response => {
|
|
|
|
|
|
|
|
// return res.json(response);
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
next(new HttpException(400, 'Cannot create spot'));
|
|
|
|
next(new HttpException(400, 'Cannot create spot'));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -111,7 +74,7 @@ class SpotifyController implements Controller {
|
|
|
|
"error": "Parameter refresh_token missing"
|
|
|
|
"error": "Parameter refresh_token missing"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var authOptions = {
|
|
|
|
let authOptions = {
|
|
|
|
method: 'POST',
|
|
|
|
method: 'POST',
|
|
|
|
url: 'https://accounts.spotify.com/api/token',
|
|
|
|
url: 'https://accounts.spotify.com/api/token',
|
|
|
|
data: qs.stringify({
|
|
|
|
data: qs.stringify({
|
|
|
@ -125,14 +88,6 @@ class SpotifyController implements Controller {
|
|
|
|
json: true
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// request.post(authOptions, function(error, response, body) {
|
|
|
|
|
|
|
|
// if (!error && response.statusCode === 200) {
|
|
|
|
|
|
|
|
// var access_token = body.access_token;
|
|
|
|
|
|
|
|
// res.send({
|
|
|
|
|
|
|
|
// 'access_token': access_token
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
axios(authOptions)
|
|
|
|
axios(authOptions)
|
|
|
|
.then(session => {
|
|
|
|
.then(session => {
|
|
|
|
if(session.status === 200){
|
|
|
|
if(session.status === 200){
|
|
|
@ -194,11 +149,10 @@ class SpotifyController implements Controller {
|
|
|
|
): Promise<Response | void> => {
|
|
|
|
): Promise<Response | void> => {
|
|
|
|
console.log("useeeee== accesToken");
|
|
|
|
console.log("useeeee== accesToken");
|
|
|
|
|
|
|
|
|
|
|
|
var code = req.query.code;
|
|
|
|
let code = req.query.code;
|
|
|
|
var state = req.query.state || null;
|
|
|
|
let state = req.query.state || null;
|
|
|
|
var storedredirectUri = req.cookies ? req.cookies[this.clientRedirect] : null;
|
|
|
|
let storedredirectUri = req.cookies ? req.cookies[this.clientRedirect] : null;
|
|
|
|
|
|
|
|
|
|
|
|
// var storedState = req.cookies ? req.cookies[stateKey] : null;
|
|
|
|
|
|
|
|
var authOptions = {
|
|
|
|
var authOptions = {
|
|
|
|
method: 'POST',
|
|
|
|
method: 'POST',
|
|
|
|
url: 'https://accounts.spotify.com/api/token',
|
|
|
|
url: 'https://accounts.spotify.com/api/token',
|
|
|
@ -217,15 +171,10 @@ class SpotifyController implements Controller {
|
|
|
|
var resp = await axios(authOptions);
|
|
|
|
var resp = await axios(authOptions);
|
|
|
|
if (resp.status === 200) {
|
|
|
|
if (resp.status === 200) {
|
|
|
|
console.log('oon esttt laaa');
|
|
|
|
console.log('oon esttt laaa');
|
|
|
|
var access_token = resp.data.access_token;
|
|
|
|
let access_token = resp.data.access_token;
|
|
|
|
var expiration =resp.data.expires_in;
|
|
|
|
let expiration =resp.data.expires_in;
|
|
|
|
var refresh = resp.data.refresh_token
|
|
|
|
let refresh = resp.data.refresh_token
|
|
|
|
console.log(access_token);
|
|
|
|
console.log(access_token);
|
|
|
|
// res.send({
|
|
|
|
|
|
|
|
// "access_token": access_token,
|
|
|
|
|
|
|
|
// "expires_in": expiration,
|
|
|
|
|
|
|
|
// "refresh" : refresh
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.clearCookie(this.clientRedirect);
|
|
|
|
res.clearCookie(this.clientRedirect);
|
|
|
|
res.redirect(`${storedredirectUri}?` +
|
|
|
|
res.redirect(`${storedredirectUri}?` +
|
|
|
|