-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
.yarn/install-state.gz | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). | ||
|
||
## Getting Started | ||
|
||
First, run the development server: | ||
|
||
```bash | ||
npm run dev | ||
# or | ||
yarn dev | ||
# or | ||
pnpm dev | ||
# or | ||
bun dev | ||
``` | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
|
||
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. | ||
|
||
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. | ||
|
||
## Learn More | ||
|
||
To learn more about Next.js, take a look at the following resources: | ||
|
||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. | ||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. | ||
|
||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! | ||
|
||
## Deploy on Vercel | ||
|
||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. | ||
|
||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CLIENT_ID="e891e4a23a36475090f934c8d39766c7" | ||
CLIENT_SECRET="b9e6536bd37e41a9b95ea5f8a1d2b7e7" | ||
REDIRECT_URI="http://localhost:3001/callback" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/** | ||
* This is an example of a basic node.js script that performs | ||
* the Authorization Code oAuth2 flow to authenticate against | ||
* the Spotify Accounts. | ||
* | ||
* For more information, read | ||
* https://developer.spotify.com/web-api/authorization-guide/#authorization_code_flow | ||
*/ | ||
require('dotenv').config(); | ||
|
||
var express = require('express'); // Express web server framework | ||
var request = require('request'); // "Request" library | ||
var cors = require('cors'); | ||
var querystring = require('querystring'); | ||
var cookieParser = require('cookie-parser'); | ||
var client_id = process.env.CLIENT_ID; // Your client id | ||
var client_secret = process.env.CLIENT_SECRET; // Your secret | ||
|
||
var redirect_uri = process.env.REDIRECT_URI; // Your redirect uri | ||
|
||
/** | ||
* Generates a random string containing numbers and letters | ||
* @param {number} length The length of the string | ||
* @return {string} The generated string | ||
*/ | ||
var generateRandomString = function (length) { | ||
var text = ''; | ||
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
|
||
for (var i = 0; i < length; i++) { | ||
text += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return text; | ||
}; | ||
|
||
var stateKey = 'spotify_auth_state'; | ||
|
||
var app = express(); | ||
|
||
app.use(express.static(__dirname + '/public')) | ||
.use(cors()) | ||
.use(cookieParser()); | ||
|
||
app.get('/login', function (req, res) { | ||
|
||
var state = generateRandomString(16); | ||
res.cookie(stateKey, state); | ||
|
||
// your application requests authorization | ||
var scope = 'user-top-read user-read-recently-played playlist-modify-public' | ||
res.redirect('https://accounts.spotify.com/authorize?' + | ||
querystring.stringify({ | ||
response_type: 'code', | ||
client_id: client_id, | ||
scope: scope, | ||
redirect_uri: redirect_uri, | ||
state: state, | ||
//show_dialog: true | ||
})); | ||
}); | ||
|
||
app.get('/callback', function (req, res) { | ||
|
||
// your application requests refresh and access tokens | ||
// after checking the state parameter | ||
|
||
var code = req.query.code || null; | ||
var state = req.query.state || null; | ||
var storedState = req.cookies ? req.cookies[stateKey] : null; | ||
|
||
if (state === null || state !== storedState) { | ||
res.redirect('/#' + | ||
querystring.stringify({ | ||
error: 'state_mismatch' | ||
})); | ||
} else { | ||
res.clearCookie(stateKey); | ||
var authOptions = { | ||
url: 'https://accounts.spotify.com/api/token', | ||
form: { | ||
code: code, | ||
redirect_uri: redirect_uri, | ||
grant_type: 'authorization_code' | ||
}, | ||
headers: { | ||
'Authorization': 'Basic ' + (Buffer.from(client_id + ':' + client_secret).toString('base64')) | ||
}, | ||
json: true | ||
}; | ||
|
||
request.post(authOptions, function (error, response, body) { | ||
if (!error && response.statusCode === 200) { | ||
|
||
var access_token = body.access_token, | ||
refresh_token = body.refresh_token; | ||
|
||
var options = { | ||
url: 'https://api.spotify.com/v1/me', | ||
headers: { 'Authorization': 'Bearer ' + access_token }, | ||
json: true | ||
}; | ||
|
||
// use the access token to access the Spotify Web API | ||
request.get(options, function (error, response, body) { | ||
//console.log(body); | ||
}); | ||
|
||
// we can also pass the token to the browser to make requests from there | ||
res.redirect('http://localhost:3000/dashboard/#' + | ||
querystring.stringify({ | ||
access_token: access_token, | ||
refresh_token: refresh_token | ||
})); | ||
} else { | ||
res.redirect('/#' + | ||
querystring.stringify({ | ||
error: 'invalid_token' | ||
})); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
app.get('/refresh_token', function (req, res) { | ||
|
||
// requesting access token from refresh token | ||
var refresh_token = req.query.refresh_token; | ||
var authOptions = { | ||
url: 'https://accounts.spotify.com/api/token', | ||
headers: { 'Authorization': 'Basic ' + (Buffer.from(client_id + ':' + client_secret).toString('base64')) }, | ||
form: { | ||
grant_type: 'refresh_token', | ||
refresh_token: refresh_token | ||
}, | ||
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 | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
const PORT = process.env.PORT || 3001 | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server listening on ${PORT}`); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.