: Revert ": change route to refresh Token, add responsibility to clients"
This reverts commit bbe6523609.
This commit is contained in:
@@ -8,22 +8,24 @@ export class SpotifyTokenGenerator {
|
||||
public createRouter() {
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/refresh-token", async (req, res) => {
|
||||
router.get("/token", async (req, res) => {
|
||||
const userService = await this.callback();
|
||||
|
||||
const user = await userService.getUserById(req.payload._id);
|
||||
const spotifyConfig = user.spotifyConfig;
|
||||
const token = await new SpotifyTokenService().refreshToke(
|
||||
spotifyConfig.refreshToken,
|
||||
);
|
||||
spotifyConfig.accessToken = token.accessToken;
|
||||
spotifyConfig.expirationDate = new Date(
|
||||
Date.now() + token.expiresIn * 1000,
|
||||
);
|
||||
user.spotifyConfig = spotifyConfig;
|
||||
await userService.updateUser(req.payload._id, user);
|
||||
const token = await new SpotifyTokenService().getToken(spotifyConfig);
|
||||
|
||||
res.status(200).send({ result: spotifyConfig });
|
||||
if (token.expiresIn !== -1) {
|
||||
console.log("Updating token");
|
||||
spotifyConfig.accessToken = token.accessToken;
|
||||
spotifyConfig.expirationDate = new Date(
|
||||
Date.now() + token.expiresIn * 1000,
|
||||
);
|
||||
user.spotifyConfig = spotifyConfig;
|
||||
await userService.updateUser(req.payload._id, user);
|
||||
}
|
||||
|
||||
res.status(200).send({ accessToken: token.accessToken });
|
||||
});
|
||||
|
||||
router.get(
|
||||
|
||||
@@ -1,46 +1,51 @@
|
||||
import {SpotifyConfig} from "../db/models/user";
|
||||
import { SpotifyConfig } from "../db/models/user";
|
||||
import axios from "axios";
|
||||
import {OAuthTokenResponse} from "../interfaces/OAuthTokenResponse";
|
||||
import { OAuthTokenResponse } from "../interfaces/OAuthTokenResponse";
|
||||
|
||||
const url = "https://accounts.spotify.com/api/token";
|
||||
const clientId = process.env.SPOTIFY_CLIENT_ID;
|
||||
const clientSecret = process.env.SPOTIFY_CLIENT_SECRET;
|
||||
|
||||
export class SpotifyTokenService {
|
||||
public async refreshToke(refreshToken: string) {
|
||||
const response = await axios.post(
|
||||
url,
|
||||
`grant_type=refresh_token&refresh_token=${refreshToken}`,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${clientId}:${clientSecret}`,
|
||||
).toString("base64")}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const token = response.data as OAuthTokenResponse;
|
||||
return {accessToken: token.access_token, expiresIn: token.expires_in};
|
||||
public async getToken(spotifyConfig: SpotifyConfig) {
|
||||
if (spotifyConfig.expirationDate > new Date()) {
|
||||
return { accessToken: spotifyConfig.accessToken, expiresIn: -1 };
|
||||
}
|
||||
console.log("Refreshing token");
|
||||
|
||||
public async generateToken(authorizationCode: string, redirectUri: string) {
|
||||
const response = await axios.post(
|
||||
url,
|
||||
`grant_type=authorization_code&code=${authorizationCode}&redirect_uri=${redirectUri}`,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${clientId}:${clientSecret}`,
|
||||
).toString("base64")}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
const response = await axios.post(
|
||||
url,
|
||||
`grant_type=refresh_token&refresh_token=${spotifyConfig.refreshToken}`,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${clientId}:${clientSecret}`,
|
||||
).toString("base64")}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
console.log(response.data);
|
||||
const token = response.data as OAuthTokenResponse;
|
||||
return { accessToken: token.access_token, expiresIn: token.expires_in };
|
||||
}
|
||||
|
||||
return response.data as OAuthTokenResponse;
|
||||
}
|
||||
public async generateToken(authorizationCode: string, redirectUri: string) {
|
||||
const response = await axios.post(
|
||||
url,
|
||||
`grant_type=authorization_code&code=${authorizationCode}&redirect_uri=${redirectUri}`,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${clientId}:${clientSecret}`,
|
||||
).toString("base64")}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
console.log(response.data);
|
||||
|
||||
return response.data as OAuthTokenResponse;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user