Skip to content

Commit

Permalink
feat: add route to get spotify access token (#168)
Browse files Browse the repository at this point in the history
* feat: add route to get spotify access token

* chore: add spotify keys

* chore: fix variable name
  • Loading branch information
120EE0692 authored Dec 20, 2022
1 parent b77ed87 commit 597c04c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 8 additions & 0 deletions server/env/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ IMAGEKIT_PRIVATE_KEY="some-key"
IMAGEKIT_URLENDPOINT="https://ik.imagekit.io/some-endpoint"

# ----- ----- END ----- -----


# ----- ----- SPOTIFY ----- -----

SPOTIFY_CLIENT_ID="some-client-id"
SPOTIFY_CLIENT_SECRET="some-client-secret"

# ----- ----- END ----- -----
30 changes: 29 additions & 1 deletion server/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const { cache } = require('../utils/userAuth/role');
const ImageKit = require('imagekit');
const UserAuth = require('../utils/userAuth');
const UserPermission = require('../utils/userAuth/permission');

const axios = require('axios');
const qs = require('qs');
/**
* @summary Express Router Object
* @description Initialize Express Router
Expand All @@ -28,6 +29,33 @@ const router = express.Router();
/** Updates roles cache */
router.use('/admin/roles/sync', async (_req, res) => res.send(await cache()));

router.use('/admin/spotify/auth', async (_req, res) => {
try {
const spotifyClientId = process.env.SPOTIFY_CLIENT_ID;
const spotifyClientSecret = process.env.SPOTIFY_CLIENT_SECRET;

const authToken = Buffer.from(`${spotifyClientId}:${spotifyClientSecret}`, 'utf-8').toString('base64');

const tokenUrl = 'https://accounts.spotify.com/api/token';
const data = qs.stringify({ grant_type: 'client_credentials' });

const response = await axios.post(tokenUrl, data, {
headers: {
Authorization: `Basic ${authToken}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
});

res.status(200).send(response.data);
} catch (error) {
return res.status(500).json({
data: 'The spotify authentication paramters could not be retrived.',
code: 500,
error,
});
}
});

router.use('/admin/media/auth', (_req, res) => {
try {
const imagekit = new ImageKit({
Expand Down

0 comments on commit 597c04c

Please sign in to comment.