-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from NOctu1412/main
Fix the token refresh error
- Loading branch information
Showing
5 changed files
with
56 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,57 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
import axios from 'axios'; | ||
import { PROXY } from '@/utils/constants'; | ||
import { GAPIKEY, PROXY } from '@/utils/constants'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
let headers_list = {"Accept": "application/json","User-Agent": "BeReal/8586 CFNetwork/1240.0.4 Darwin/20.6.0","x-ios-bundle-identifier": "AlexisBarreyat.BeReal","Content-Type": "application/json"} | ||
let firebase_refresh_token = req.body.refresh; | ||
|
||
let headersList = { | ||
"Accept": "*/*", | ||
"User-Agent": "BeReal/8586 CFNetwork/1240.0.4 Darwin/20.6.0", | ||
"x-ios-bundle-identifier": "AlexisBarreyat.BeReal", | ||
"Content-Type": "application/json" | ||
let firebase_refresh_data = JSON.stringify({ | ||
"grantType": "refresh_token", | ||
"refreshToken": firebase_refresh_token | ||
}); | ||
let firebase_refresh_options = { | ||
url: `https://securetoken.googleapis.com/v1/token?key=${GAPIKEY}`, | ||
method: "POST", | ||
headers: headers_list, | ||
data: firebase_refresh_data, | ||
} | ||
let firebase_refresh_response = await axios.request(firebase_refresh_options); | ||
|
||
let refresh_token = req.body.refresh; | ||
let new_firebase_token = firebase_refresh_response.data.id_token; | ||
let new_firebase_refresh_token = firebase_refresh_response.data.refresh_token; | ||
let firebase_expiration = Date.now() + firebase_refresh_response.data.expires_in * 1000; | ||
|
||
console.log("refresh token") | ||
console.log(refresh_token); | ||
// ============================================================================================ | ||
|
||
let refresh_body = JSON.stringify({ | ||
"grant_type": "refresh_token", | ||
let access_grant = JSON.stringify({ | ||
"grant_type": "firebase", | ||
"client_id": "ios", | ||
"client_secret": "962D357B-B134-4AB6-8F53-BEA2B7255420", | ||
"refresh_token": refresh_token | ||
"token": new_firebase_token | ||
}); | ||
|
||
let refresh_options = { | ||
url: `${PROXY}https://auth.bereal.team/token?grant_type=refresh_token`, | ||
let access_grant_options = { | ||
url: `${PROXY}https://auth.bereal.team/token?grant_type=firebase`, | ||
method: "POST", | ||
headers: headersList, | ||
data: refresh_body, | ||
headers: headers_list, | ||
data: access_grant, | ||
} | ||
|
||
return axios.request(refresh_options).then( | ||
return await axios.request(access_grant_options).then( | ||
(response) => { | ||
console.log(response.data); | ||
|
||
let token = response.data.access_token; | ||
let refresh = response.data.refresh_token; | ||
let expiration = Date.now() + response.data.expires_in * 1000; | ||
|
||
res.status(200).json({ status: "success", token: token, refresh: refresh, expiration: expiration }); | ||
let bereal_access_token = response.data.access_token; | ||
res.status(200).json({ | ||
status: "success", | ||
token: bereal_access_token, | ||
firebase_id_token: new_firebase_token, | ||
firebase_refresh_token: new_firebase_refresh_token, | ||
expiration: firebase_expiration | ||
}) | ||
} | ||
).catch( | ||
(error) => { | ||
console.log("ERROR"); | ||
console.log(error); | ||
res.status(400).json({ status: "error" }); | ||
} | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters