백엔드 서버를 사용하여 로그인한 유저를 안전하게 확인하는 방법입니다.
userData.getUserId() 호출 시 반환받는 유저 ID 등을 백엔드 서버에서 허용하시면 안 됩니다.HTTPS POST를 사용하여 백엔드 서버의 tokensignin API에 ID 토큰을 보냅니다.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://yourbackend.example.com/tokensignin");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("idToken", idToken));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
final String responseBody = EntityUtils.toString(response.getEntity());
Log.i(TAG, "Signed in as: " + responseBody);
} catch (ClientProtocolException e) {
Log.e(TAG, "Error sending ID token to backend.", e);
} catch (IOException e) {
Log.e(TAG, "Error sending ID token to backend.", e);
}
백엔드 서버에서 HTTPS POST를 통해 ID 토큰을 받은 후 토큰 인증 API를 사용하여 토큰을 인증합니다.
import requests
try:
url = "https://now.gg/accounts/oauth2/v1/verify-token"
payload={
"token_type": "id_token",
"token": < id_token > ,
"client_id": < your_oauth_client_id >
}
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
print(response.text)
#token verified
except:
#token not verified
pass
var axios = require('axios');
var config = {
method: 'post',
url: 'https://now.gg/accounts/oauth2/v1/verify-token',
headers: {
'Content-Type': 'application/json'
},
data: {
"token_type": "id_token",
"token": < id_token > ,
"client_id": < your_oauth_client_id >
}
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
토큰을 인증한 후 유저가 이미 데이터베이스에 있는지 확인합니다.
중요: 새로 생성된 유저를 데이터베이스에 저장 시 필요한 추가 유저 프로필 정보를 호출하실 수 있습니다.
유저 계정 서비스
유저 계정 서비스
문서 Rev. 1.0