Skip to content

Commit 7d02c0f

Browse files
committed
auth middleware
1 parent 5a093b3 commit 7d02c0f

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

controllers/authController.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
const spotifyService = require('../services/spotifyService');
2-
const createSpotifyApi = require('../utils/spotifyApi');
2+
const spotifyApi = require('../utils/spotifyApi');
33

44
const login = (req, res) => {
5-
const spotifyApi = createSpotifyApi();
6-
const authUrl = spotifyApi.createAuthorizeURL(scopes, 'state-key');
5+
const authUrl = spotifyService.getAuthUrl();
76
res.redirect(authUrl);
87
};
98

109
const callback = async (req, res) => {
11-
const spotifyApi = createSpotifyApi();
1210
try {
1311
const code = req.query.code || null;
1412
const data = await spotifyApi.authorizationCodeGrant(code);
1513
const { access_token, refresh_token } = data.body;
1614

17-
res.cookie('spotify_access_token', access_token, { httpOnly: true });
18-
res.cookie('spotify_refresh_token', refresh_token, { httpOnly: true });
15+
res.cookie('spotify_access_token', access_token, {
16+
httpOnly: true,
17+
secure: true,
18+
sameSite: 'lax'
19+
});
1920

21+
res.cookie('spotify_refresh_token', refresh_token, {
22+
httpOnly: true,
23+
secure: true,
24+
sameSite: 'lax'
25+
});
26+
2027
res.redirect('https://main.d1n7z7zw3v28b1.amplifyapp.com/home');
2128
} catch (error) {
29+
console.error('Auth callback error:', error);
2230
res.redirect('https://main.d1n7z7zw3v28b1.amplifyapp.com/login?error=auth_failed');
2331
}
2432
};
2533

2634
module.exports = {
2735
login,
2836
callback,
29-
};
37+
};

middleware/authMiddleware.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
const spotifyApi = require('../utils/spotifyApi');
2+
13
const auth = (req, res, next) => {
24
const token = req.cookies.spotify_access_token;
35

46
if (!token) {
5-
return res.status(401).json({ error: 'Authentication required' });
7+
return res.status(401).json({ error: 'Authentication required' });
68
}
7-
9+
10+
spotifyApi.setAccessToken(token);
811
req.spotifyToken = token;
912
next();
10-
};
11-
12-
module.exports = auth;
13+
};
14+
15+
module.exports = auth;

routes/playlistRoutes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ const playlistController = require('../controllers/playlistController');
44
const auth = require('../middleware/authMiddleware');
55

66
const router = express.Router();
7+
router.use(auth);
78

89
router.get('/liked-songs', playlistController.getLikedSongs);
910
router.get('/', playlistController.getPlaylists);
1011
router.get('/:playlistId', playlistController.getPlaylistSongs);
11-
router.post('/create', auth, playlistController.createPlaylist);
12+
router.post('/create', playlistController.createPlaylist);
1213

1314

1415

services/spotifyService.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const createSpotifyApi = require('../utils/spotifyApi');
1+
const spotifyApi = require('../utils/spotifyApi');
22

33
const path = require('path');
44
const playlistImages = [
@@ -18,18 +18,16 @@ const convertImageToBase64 = async (imagePath) => {
1818

1919
const getAuthUrl = () => {
2020
const scopes = ['user-library-read', 'playlist-modify-private', 'playlist-modify-public'];
21-
return createSpotifyApi.createAuthorizeURL(scopes, 'state-key');
21+
return spotifyApi.createAuthorizeURL(scopes, 'state-key');
2222
};
2323

2424
const setAccessToken = (accessToken) => {
25-
createSpotifyApi.setAccessToken(accessToken);
25+
spotifyApi.setAccessToken(accessToken);
2626
};
2727

28-
const getUserPlaylists = async (token, offset = 0) => {
29-
const spotifyApi = createSpotifyApi();
30-
spotifyApi.setAccessToken(token);
31-
28+
const getUserPlaylists = async (offset = 0) => {
3229
const playlistResponse = await spotifyApi.getUserPlaylists({ offset });
30+
3331
const likedSongsResponse = await spotifyApi.getMySavedTracks();
3432

3533
return {
@@ -40,7 +38,7 @@ const getUserPlaylists = async (token, offset = 0) => {
4038

4139
const getLikedSongs = async () => {
4240
try {
43-
const response = await createSpotifyApi.getMySavedTracks({
41+
const response = await spotifyApi.getMySavedTracks({
4442
limit: 50,
4543
offset: 0
4644
});
@@ -56,19 +54,19 @@ const getLikedSongs = async () => {
5654

5755

5856
const getPlaylist = async (playlistId) => {
59-
const response = await createSpotifyApi.getPlaylist(playlistId);
57+
const response = await spotifyApi.getPlaylist(playlistId);
6058
return response.body;
6159
};
6260

6361
const getPlaylistTracks = async (playlistId) => {
64-
const response = await createSpotifyApi.getPlaylistTracks(playlistId);
62+
const response = await spotifyApi.getPlaylistTracks(playlistId);
6563
return response.body;
6664
};
6765

6866
const getTrackRecommendations = async (seedTrackId) => {
69-
const seedTrackFeatures = await createSpotifyApi.getAudioFeaturesForTrack(seedTrackId);
67+
const seedTrackFeatures = await spotifyApi.getAudioFeaturesForTrack(seedTrackId);
7068

71-
const response = await createSpotifyApi.getRecommendations({
69+
const response = await spotifyApi.getRecommendations({
7270
seed_tracks: [seedTrackId],
7371
limit: 100,
7472
target_instrumentalness: seedTrackFeatures.body.instrumentalness,
@@ -102,25 +100,25 @@ const getTrackRecommendations = async (seedTrackId) => {
102100
};
103101

104102
const createPlaylist = async (userId, name, description, trackUris) => {
105-
const playlistResponse = await createSpotifyApi.createPlaylist(userId, {
103+
const playlistResponse = await spotifyApi.createPlaylist(userId, {
106104
name,
107105
description,
108106
});
109107

110108
const playlistId = playlistResponse.body.id;
111-
await createSpotifyApi.addTracksToPlaylist(playlistId, trackUris);
109+
await spotifyApi.addTracksToPlaylist(playlistId, trackUris);
112110

113111
const randomImageIndex = Math.floor(Math.random() * playlistImages.length);
114112
const imagePath = playlistImages[randomImageIndex];
115113

116114
const imageData = await convertImageToBase64(imagePath);
117-
await createSpotifyApi.uploadCustomPlaylistCoverImage(playlistId, imageData);
115+
await spotifyApi.uploadCustomPlaylistCoverImage(playlistId, imageData);
118116

119117
return playlistId;
120118
};
121119

122120
const createPlaylistFromSeedTrack = async (userId, seedTrackId) => {
123-
const seedTrack = await createSpotifyApi.getTrack(seedTrackId);
121+
const seedTrack = await spotifyApi.getTrack(seedTrackId);
124122
const seedTrackName = seedTrack.body.name;
125123

126124
const recommendations = await getTrackRecommendations(seedTrackId);

utils/spotifyApi.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
const SpotifyWebApi = require('spotify-web-api-node');
22

3-
const createSpotifyApi = () => {
4-
return new SpotifyWebApi({
5-
clientId: '5e3eef3570b74a37af3438268b820e32',
6-
clientSecret: 'ecda63e51490449d9c94b26f9fd9571a',
7-
redirectUri: 'https://groovz-backend-js.onrender.com/auth/callback',
8-
});
9-
};
3+
const spotifyApi = new SpotifyWebApi({
4+
clientId: '5e3eef3570b74a37af3438268b820e32',
5+
clientSecret: 'ecda63e51490449d9c94b26f9fd9571a',
6+
redirectUri: 'https://groovz-backend-js.onrender.com/auth/callback',
7+
});
108

11-
module.exports = createSpotifyApi;
9+
module.exports = spotifyApi;

0 commit comments

Comments
 (0)