Skip to content

Instantly share code, notes, and snippets.

@joelcloralt
Last active February 7, 2024 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelcloralt/7178b8694a768fbdfd6e504eee52086a to your computer and use it in GitHub Desktop.
Save joelcloralt/7178b8694a768fbdfd6e504eee52086a to your computer and use it in GitHub Desktop.
Serverless function to handle oauth
const axios = require('axios');
const qs = require('qs');
exports.handler = async function(event, context) {
// Parse the 'code' from the query string
const { code } = event.queryStringParameters;
// Token endpoint for your service, this should be replaced with your actual token endpoint
const tokenURL = 'https://auth.smartcar.com/oauth/token';
// The client_id and client_secret for your application on the service
const client_id = process.env.CLIENT_ID;
const client_secret = process.env.CLIENT_SECRET;
try {
// Send a POST request to the token endpoint
const response = await axios.post(
tokenURL,
qs.stringify({
client_id,
client_secret,
code,
grant_type: 'authorization_code',
redirect_uri: `${process.env.REDIRECT_URI}/api/oauth-callback`, //must match in Smartcar dashboard
}),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);
console.log('response...', response.data);
// Extract the access_token from the response
const { access_token, refresh_token, expires_in } = response.data;
// When the token is received
// Redirect to the specified URL with the token appended as a query parameter
return {
statusCode: 302,
headers: {
Location: `${process.env.REDIRECT_URI}?access_token=${access_token}&refresh_token=${refresh_token}&expires_in=${expires_in}`
}
};
} catch (error) {
console.error('Error exchanging code for token:', error);
// Respond with a 500 status code (Server Error) if anything went wrong
return {
statusCode: 500,
body: JSON.stringify({ error: 'Server Error' })
};
}
};
const axios = require('axios');
const qs = require('qs');
exports.handler = async function(event, context) {
const { code } = event.queryStringParameters;
const tokenURL = 'https://auth.smartcar.com/oauth/token';
const client_id = process.env.DEMO_CLIENT_ID;
const client_secret = process.env.DEMO_CLIENT_SECRET;
const redirect_uri = process.env.DEMO_REDIRECT_URI;
console.log('code...', code);
console.log('client_id...', client_id);
console.log('client_secret...', client_secret);
console.log('redirect_uri...', redirect_uri);
try {
const response = await axios.post(
tokenURL,
qs.stringify({
client_id,
client_secret,
code,
grant_type: 'authorization_code',
redirect_uri,
}),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);
console.log('response...', response.data);
const { access_token, refresh_token, expires_in } = response.data;
return {
statusCode: 200,
body: JSON.stringify({ access_token, refresh_token, expires_in })
}
} catch (error) {
console.error('Error exchanging code for token:', error);
// Respond with a 500 status code (Server Error) if anything went wrong
return {
statusCode: 500,
body: JSON.stringify({ error: 'Server Error' })
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment