Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async onToken(token) { // Token returned from Stripe
const res = await fetch(config.stripe.apiUrl, { // Backend API url
method: 'POST',
body: JSON.stringify({
token,
charge: {
amount: this.props.amount,
currency: config.stripe.currency,
},
}),
});
const data = await res.json();
console.log('onToken'); // Logs for ease of debugging
console.log(data);
}
function fetchQuery(operation, variables, cacheConfig, uploadables) {
const endpoint = 'http://localhost:3000/graphql';
return fetch(endpoint, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
}, // Add authentication and other headers here
body: JSON.stringify({
query: operation.text, // GraphQL text from input
variables,
}),
})
.then(response => response.json())
.catch(error => console.log('errrrr', error));
}
async fetchUsers() {
try {
this.requestUsers()
const response = await fetch('https://api.github.com/users')
const users = await response.json()
this.receiveUsers(users)
} catch (err) {
console.log(err)
this.receiveUsers([])
}
},
},
function fetchJson(urlPath: string, options?: Object): Promise {
return fetch(getApiUrl(urlPath), options).then(parseResponse);
}
export async function request(url) {
const options = {
mode: 'cors',
method: 'GET'
}
const response = await fetch(`${BAN_DASHBOARD_URL || 'https://adresse.data.gouv.fr/data'}${url}`, options)
if (!response.ok) {
throw new HttpError(response)
}
if (response.ok) {
return response.json()
}
throw new Error('Une erreur est survenue')
}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
//if the authtoken is not found
if(typeof c.authtoken == 'undefined') {
//don't do anything if we are on a page that doesn't require credentials
if(ctx.pathname == "/login" || ctx.pathname == "/forgot-password") return {pageProps};
//if we are on any other page, redirect to the login page
else redirectTo('/login', { res: ctx.res, status: 301 })
}
//if we do have an auth token to check
else {
var response = await fetch(API_URL+'/auth', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token:c.authtoken} )
})
.then( r => r.json() )
.then( resp => {
if(ctx.pathname == "/") {
//if auth check was successful, send to dashboard
if(resp.result == "success") redirectTo('/dashboard', { res: ctx.res, status: 301 })
else {
//setting the cookie to expire way back when removes it
document.cookie = "authtoken=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
redirectTo('/login', { res: ctx.res, status: 301 })
static async getInitialProps() {
const res = await fetch("https://api.tvmaze.com/search/shows?q=marvel");
const data = await res.json();
return {
shows: data
};
}