Khanh LeViet
Developer Advocate
ã½ã¼ã·ã£ã« ãã°ã¤ã³ã好ãã§å©ç¨ããã¦ã¼ã¶ã¼ã¯æè¿ç¹ã«å¢ãã¦ãã¦ãã¾ã。Google ãã°ã¤ã³ã®ã»ã、Facebook、Twitter ãªã©ã¨ã®ãã°ã¤ã³é£æºã使ãã¨、使ç¨ãããµã¼ãã¹ãã¨ã«æ°ããã¢ã«ã¦ã³ããä½æããå¿
è¦ããªããªãã¾ã。ã¦ã¼ã¶ã¼ã«ã¯ãããããæ°ã«å
¥ãã®ã½ã¼ã·ã£ã« ãã°ã¤ã³ ãããã¤ããããã¾ãã、ã¢ããªã«åãã°ã¤ã³é£æºãããã¤ããçµã¿è¾¼ãã§ç®¡çããã®ã¯、å¤å¤§ãªå´åãå¿
è¦ã§ã。ãã®ä½æ¥ã楽ã«ããã®ã Firebase Authentication ã§ã。
Firebase Authentication ã¯、å¤ãã®æåãªã½ã¼ã·ã£ã« ãã°ã¤ã³ ãããã¤ãã«å¯¾å¿ãã¦ãã¾ã。ã¢ããªã« Firebase Authentication ãçµã¿è¾¼ã¿ããããã°、Firebase ã¯è£ã§èªåçã«è¤æ°ã®ãã°ã¤ã³é£æºãããã¤ãã管çãã¦ããã¾ã。
ããã、ã¦ã¼ã¶ã¼ã®å±
ä½å½ã«ãã£ã¦ã¯、ã¾ã Firebase ãããã©ã«ãã§å¯¾å¿ãã¦ããªããã°ã¤ã³é£æºãããã¤ãã好ã¾ãã¦ããããããã¾ãã。ãã¨ãã°、æ¥æ¬ãã¢ã¸ã¢ã®å½ã
ã§ã¯、å¤ãã®ã¦ã¼ã¶ã¼ãæã¤
LINE ãã°ã¤ã³ ãå¹
åºãæ®åãã¦ããã®ã§、ãã®ãµãã¼ããæå¾
ããæ¹ãããã£ãããã§ããã。ãããããã¨ã«、Firebase Authentication ã®ã«ã¹ã¿ã èªè¨¼ã使ãã¨、LINE ãã°ã¤ã³ãã¯ããã¨ããå¤ãã®ãã°ã¤ã³é£æºãããã¤ããç°¡åã«ãµãã¼ããããã¨ãã§ãã¾ã。
以åã®
ããã°æ稿 ã§ã¯、ã¦ã§ã㨠JavaScript SDK ã使ã£ã¦ Firebase Authentication ãããã©ã«ãã§å¯¾å¿ãã¦ããªããã°ã¤ã³é£æºã«å¯¾å¿ããæ¹æ³ãç´¹ä»ãã¾ãã。ä»åã®ããã°æ稿ã§ã¯、iOS 㨠Android ã« LINE ãã°ã¤ã³ãçµã¿è¾¼ãæ¹æ³ãç´¹ä»ãã¾ããã。
è¨è¨ã®æ¦è¦
ãã°ã¤ã³ã®ããã¼ã¯、次ã®ãããªæµãã§å®è¡ããã¾ã。
ã¹ããã 1: LINE Login SDK ã使ã£ã¦ãã°ã¤ã³ã、ã¦ã¼ã¶ã¼ã® LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãåå¾ãã¾ã。
ã¹ããã 2: LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãèªåã®ãµã¼ãã¼ã«éä¿¡ã、LINE èªè¨¼ãµã¼ãã¼ã使ã£ã¦ãããæ¤è¨¼ãã¾ã。ãã¼ã¯ã³ãæå¹ã§ããå ´å、ã¦ã¼ã¶ã¼ã«å¯¾å¿ãã Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãä½æã、ã¦ã¼ã¶ã¼ã®ç«¯æ«ã«è¿ãã¾ã。
ã¹ããã 3: Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ã使ã£ã¦、端æ«ãã Firebase ã«ãã°ã¤ã³ãã¾ã。
æ§ç¯ã«çæãã
æºå
LINE ãã¸ãã¹ ã¢ã«ã¦ã³ããåå¾ã㦠Firebase ããã¸ã§ã¯ããæºåããã«ã¯、ããã¤ãã®è¨å®ä½æ¥ãå¿
è¦ã«ãªãã¾ã。
ãã¡ã ã®æé ã«ãããã£ã¦ LINE ãã¸ãã¹ ã¢ã«ã¦ã³ããè¨å®ã、ã¢ããªã« LINE SDK ãçµã¿è¾¼ã¿ã¾ã。
次ã«、ã¢ããªã« Firebase Authentication ã©ã¤ãã©ãªã追å ããã¦ãããã¨ã確èªãã¾ã(iOS / Android )。
ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ã®çæã« Firebase Server SDK ã使ããã¨ãã§ãã¾ãã、ä»»æã® JSON ã¦ã§ããã¼ã¯ã³ ã©ã¤ãã©ãªã使ã£ã¦çæãããã¨ãã§ãããã、ããã¯å¿
é ã§ã¯ããã¾ãã。
LINE ãã°ã¤ã³ããã¼ã®éå§
LINE ãã°ã¤ã³ã®ããã¥ã¡ã³ã(
iOS /
Android )ãåç
§ã、ã¢ããªã« LINE SDK ãçµã¿è¾¼ã㧠LINE ãã°ã¤ã³ ããã¼ãå®è£
ãã¾ã。ã¦ã¼ã¶ã¼ã®ãã°ã¤ã³ãæåããã¨、次ã®ããã«ã㦠LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãåå¾ã§ãã¾ã。
iOS(Objective-C)
NSString *lineAccessToken = self.lineAdapter.getLineApiClient.accessToken;
Android
LineAuthManager authManager = LineSdkContextManager.getSdkContext().getAuthManager();
final String accessToken = authManager.getAccessToken().accessToken;
次ã«、ã好ã¿ã®ãããã¯ã¼ã¯éä¿¡ã©ã¤ãã©ãªã使ã£ã¦、èªåã®ãµã¼ãã¼ã«ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãéä¿¡ãã¦æ¤è¨¼ãã¾ã。ä»åã®ãµã³ãã«ã³ã¼ãã§ã¯、iOS ã¯
GTM HTTP Fetcher ã使ã、Android ã¯
Volley ã使ã£ã¦ãã¾ã。
iOS (Objective-C)
NSURL *url = [NSURL URLWithString:@"https:///verifyToken"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
NSDictionary *token = @{@"token" : lineAccessToken};
NSError *error;
NSData *requestBody = [NSJSONSerialization dataWithJSONObject:token
options:kNilOptions error:&error];
[request setHTTPBody:requestBody];
GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (!error) {
// ã¬ã¹ãã³ã¹ãã Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãæ½åº
// ・・・
}
}];
Android
HashMap<string, string=""> validationObject = new HashMap<>();
validationObject.put("token", accessToken);
Response.Listener responseListener = new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
// ã¬ã¹ãã³ã¹ãã Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãæ½åº
// ・・・
}
};
JsonObjectRequest fbTokenRequest = new JsonObjectRequest(
Request.Method.POST,
"https:///verifyToken",
new JSONObject(validationObject),
responseListener, errorListener);
NetworkSingleton.getInstance(activity).addToRequestQueue(fbTokenRequest); </string,>
LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ã¨ Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ã®äº¤æ
LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãæ¤è¨¼ã、ã¦ã¼ã¶ã¼ã«å¯¾å¿ãã Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãçæããã«ã¯、ãµã¼ãã¼ãå¿
è¦ã«ãªãã¾ã。Firebase Node.js Server SDK 㨠Express ã¦ã§ããµã¼ãã¼ã使ãã¨、ã·ã³ãã«ãªãµã¼ãã¼ãæ§ç¯ã§ãã¾ã。
ã¾ã、ãµã¼ãã¼ã¯ã¦ã¼ã¶ã¼ã®ç«¯æ«ãã LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãåä¿¡ã、LINE Social Rest API ã使ã£ã¦ãããæ¤è¨¼ãã¾ã。API ã®ã¬ã¹ãã³ã¹ã§å¿ããã«
channelId ã®å¤ã確èªã、ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãã¢ããªããçºè¡ããã¦ãããã¨ãæ¤è¨¼ãã¦ãã ãã。ããã¯ä»ã®ã¢ããªããã£ã³ãã«ã®ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãåå©ç¨ãã¦ã¢ããªã«ãã°ã¤ã³ãããã¨ãããªããã¾ãæ»æãé²ãããã§ã。
ãµã¼ãã¼(Node.js)
app.post('/verifyToken', (req, res) => {
if (!req.body.token) {
return res.status(400).send('Access Token not found');
}
const reqToken = req.body.token;
// ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãæ¤è¨¼ããããã« LINE ãµã¼ãã¼ã«ãªã¯ã¨ã¹ããéä¿¡
const options = {
url: 'https://api.line.me/v1/oauth/verify',
headers: {
'Authorization': `Bearer ${reqToken}`
}
};
request(options, (error, response, body) => {
if (!error && response.statusCode === 200) {
const lineObj = JSON.parse(body);
// ãªããã¾ãæ»æãé²ãããã«å¿
ããã¼ã¯ã³ã® channelId ã確èª
if ((typeof lineObj.mid !== 'undefined')
&& (lineObj.channelId === myLINEChannelId)) {
// LINE ãµã¼ãã¼ã«ããã¢ã¯ã»ã¹ ãã¼ã¯ã³ã®æ¤è¨¼ãæå
// Firebase ãã¼ã¯ã³ãçæã、端æ«ã«è¿å´
const firebaseToken = generateFirebaseToken(lineObj.mid);
// LINE ãããã¡ã¤ã«ã§ Firebase ã¦ã¼ã¶ã¼ ãããã¡ã¤ã«ãã¢ãããã¼ã
updateUserProfile(reqToken, firebaseToken, lineObj.mid, () => {
const ret = {
firebase_token: firebaseToken
};
return res.status(200).send(ret);
});
}
}
const ret = {
error_message:'Authentication error:Cannot verify access token.'
};
return res.status(403).send(ret);
});
}
});
LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ã®æ¤è¨¼ãæåããã、Firebase Server SDK ã使ã£ã¦ Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãçæã、ã¦ã¼ã¶ã¼ã®ç«¯æ«ã«è¿ãã¾ã。LINE ã®ã¦ã¼ã¶ã¼ ID ã¯、Firebase ã®ã¦ã¼ã¶ã¼ ID ã¨ãã¦åå©ç¨ã§ãã¾ã。
ãµã¼ãã¼(Node.js)
function generateFirebaseToken(lineMid) {
var firebaseUid = 'line:' + lineMid;
var additionalClaims = {
provider:'LINE'
};
return firebase.auth().createCustomToken(firebaseUid);
}
Firebase Server
Java SDK ã使ããã¨ãã§ãã¾ã。
App Engine Flexible Environment ã
Cloud Functions ã®ãããªã½ãªã¥ã¼ã·ã§ã³ã使ãã¨、ãµã¼ãã¼ã管çããå¿
è¦ã¯ãªããªãã¾ã。
ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ã«ãã Firebase ã¸ã®ãã°ã¤ã³
Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãåä¿¡ããã、ããã使ã£ã¦ Firebase ã«ãã°ã¤ã³ãã¾ã。
iOS(Objective-C)
[[FIRAuth auth] signInWithCustomToken:firebaseToken
completion:^(FIRUser * _Nullable user, NSError * _Nullable error) {
// ãã°ã¤ã³çµæã®å¦ç
// ・・・
}];
Android
FirebaseAuth.getInstance()
.signInWithCustomToken(firebaseToken)
.addOnCompleteListener(new OnCompleteListener() {
// ãã°ã¤ã³çµæã®å¦ç
// ・・・
});
試ãã¦ã¿ã
ãµã³ãã«ã³ã¼ãã¯ãªã¼ãã³ã½ã¼ã¹ã§ã。ãã²ãã¦ã³ãã¼ããã¦ã試ããã ãã。
https://github.com/firebase/custom-auth-samples/tree/master/Line
Posted by
Khanh LeViet - Developer Relations Team
[ãã®è¨äºã¯ Khanh LeViet、ãããããã¼ ã¢ããã±ã¼ãã«ãã The Firebase Blog ã®è¨äº "Authenticate your Firebase users with LINE Login " ãå
ã«ç¿»è¨³・å çãããã®ã§ã。詳ããã¯å
è¨äºãã覧ãã ãã。]
Khanh LeViet
Developer Advocate
ã½ã¼ã·ã£ã« ãã°ã¤ã³ã好ãã§å©ç¨ããã¦ã¼ã¶ã¼ã¯æè¿ç¹ã«å¢ãã¦ãã¦ãã¾ã。Google ãã°ã¤ã³ã®ã»ã、Facebook、Twitter ãªã©ã¨ã®ãã°ã¤ã³é£æºã使ãã¨、使ç¨ãããµã¼ãã¹ãã¨ã«æ°ããã¢ã«ã¦ã³ããä½æããå¿
è¦ããªããªãã¾ã。ã¦ã¼ã¶ã¼ã«ã¯ãããããæ°ã«å
¥ãã®ã½ã¼ã·ã£ã« ãã°ã¤ã³ ãããã¤ããããã¾ãã、ã¢ããªã«åãã°ã¤ã³é£æºãããã¤ããçµã¿è¾¼ãã§ç®¡çããã®ã¯、å¤å¤§ãªå´åãå¿
è¦ã§ã。ãã®ä½æ¥ã楽ã«ããã®ã Firebase Authentication ã§ã。
Firebase Authentication ã¯、å¤ãã®æåãªã½ã¼ã·ã£ã« ãã°ã¤ã³ ãããã¤ãã«å¯¾å¿ãã¦ãã¾ã。ã¢ããªã« Firebase Authentication ãçµã¿è¾¼ã¿ããããã°、Firebase ã¯è£ã§èªåçã«è¤æ°ã®ãã°ã¤ã³é£æºãããã¤ãã管çãã¦ããã¾ã。
ããã、ã¦ã¼ã¶ã¼ã®å±
ä½å½ã«ãã£ã¦ã¯、ã¾ã Firebase ãããã©ã«ãã§å¯¾å¿ãã¦ããªããã°ã¤ã³é£æºãããã¤ãã好ã¾ãã¦ããããããã¾ãã。ãã¨ãã°、æ¥æ¬ãã¢ã¸ã¢ã®å½ã
ã§ã¯、å¤ãã®ã¦ã¼ã¶ã¼ãæ㤠LINE ãã°ã¤ã³ ãå¹
åºãæ®åãã¦ããã®ã§、ãã®ãµãã¼ããæå¾
ããæ¹ãããã£ãããã§ããã。ãããããã¨ã«、Firebase Authentication ã®ã«ã¹ã¿ã èªè¨¼ã使ãã¨、LINE ãã°ã¤ã³ãã¯ããã¨ããå¤ãã®ãã°ã¤ã³é£æºãããã¤ããç°¡åã«ãµãã¼ããããã¨ãã§ãã¾ã。
以åã®ããã°æ稿 ã§ã¯、ã¦ã§ã㨠JavaScript SDK ã使ã£ã¦ Firebase Authentication ãããã©ã«ãã§å¯¾å¿ãã¦ããªããã°ã¤ã³é£æºã«å¯¾å¿ããæ¹æ³ãç´¹ä»ãã¾ãã。ä»åã®ããã°æ稿ã§ã¯、iOS 㨠Android ã« LINE ãã°ã¤ã³ãçµã¿è¾¼ãæ¹æ³ãç´¹ä»ãã¾ããã。
è¨è¨ã®æ¦è¦
ãã°ã¤ã³ã®ããã¼ã¯、次ã®ãããªæµãã§å®è¡ããã¾ã。
ã¹ããã 1: LINE Login SDK ã使ã£ã¦ãã°ã¤ã³ã、ã¦ã¼ã¶ã¼ã® LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãåå¾ãã¾ã。
ã¹ããã 2: LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãèªåã®ãµã¼ãã¼ã«éä¿¡ã、LINE èªè¨¼ãµã¼ãã¼ã使ã£ã¦ãããæ¤è¨¼ãã¾ã。ãã¼ã¯ã³ãæå¹ã§ããå ´å、ã¦ã¼ã¶ã¼ã«å¯¾å¿ãã Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãä½æã、ã¦ã¼ã¶ã¼ã®ç«¯æ«ã«è¿ãã¾ã。
ã¹ããã 3: Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ã使ã£ã¦、端æ«ãã Firebase ã«ãã°ã¤ã³ãã¾ã。
æ§ç¯ã«çæãã
æºå
LINE ãã¸ãã¹ ã¢ã«ã¦ã³ããåå¾ã㦠Firebase ããã¸ã§ã¯ããæºåããã«ã¯、ããã¤ãã®è¨å®ä½æ¥ãå¿
è¦ã«ãªãã¾ã。
ãã¡ã ã®æé ã«ãããã£ã¦ LINE ãã¸ãã¹ ã¢ã«ã¦ã³ããè¨å®ã、ã¢ããªã« LINE SDK ãçµã¿è¾¼ã¿ã¾ã。
次ã«、ã¢ããªã« Firebase Authentication ã©ã¤ãã©ãªã追å ããã¦ãããã¨ã確èªãã¾ã(iOS / Android )。
ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ã®çæã« Firebase Server SDK ã使ããã¨ãã§ãã¾ãã、ä»»æã® JSON ã¦ã§ããã¼ã¯ã³ ã©ã¤ãã©ãªã使ã£ã¦çæãããã¨ãã§ãããã、ããã¯å¿
é ã§ã¯ããã¾ãã。
LINE ãã°ã¤ã³ããã¼ã®éå§
LINE ãã°ã¤ã³ã®ããã¥ã¡ã³ã(iOS / Android )ãåç
§ã、ã¢ããªã« LINE SDK ãçµã¿è¾¼ã㧠LINE ãã°ã¤ã³ ããã¼ãå®è£
ãã¾ã。ã¦ã¼ã¶ã¼ã®ãã°ã¤ã³ãæåããã¨、次ã®ããã«ã㦠LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãåå¾ã§ãã¾ã。
iOS(Objective-C)
NSString *lineAccessToken = self.lineAdapter.getLineApiClient.accessToken;
Android
LineAuthManager authManager = LineSdkContextManager.getSdkContext().getAuthManager();
final String accessToken = authManager.getAccessToken().accessToken;
次ã«、ã好ã¿ã®ãããã¯ã¼ã¯éä¿¡ã©ã¤ãã©ãªã使ã£ã¦、èªåã®ãµã¼ãã¼ã«ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãéä¿¡ãã¦æ¤è¨¼ãã¾ã。ä»åã®ãµã³ãã«ã³ã¼ãã§ã¯、iOS 㯠GTM HTTP Fetcher ã使ã、Android 㯠Volley ã使ã£ã¦ãã¾ã。
iOS (Objective-C)
NSURL *url = [NSURL URLWithString:@"https:///verifyToken"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
NSDictionary *token = @{@"token" : lineAccessToken};
NSError *error;
NSData *requestBody = [NSJSONSerialization dataWithJSONObject:token
options:kNilOptions error:&error];
[request setHTTPBody:requestBody];
GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (!error) {
// ã¬ã¹ãã³ã¹ãã Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãæ½åº
// ・・・
}
}];
Android
HashMap<string, string=""> validationObject = new HashMap<>();
validationObject.put("token", accessToken);
Response.Listener responseListener = new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
// ã¬ã¹ãã³ã¹ãã Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãæ½åº
// ・・・
}
};
JsonObjectRequest fbTokenRequest = new JsonObjectRequest(
Request.Method.POST,
"https:///verifyToken",
new JSONObject(validationObject),
responseListener, errorListener);
NetworkSingleton.getInstance(activity).addToRequestQueue(fbTokenRequest); </string,>
LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ã¨ Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ã®äº¤æ
LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãæ¤è¨¼ã、ã¦ã¼ã¶ã¼ã«å¯¾å¿ãã Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãçæããã«ã¯、ãµã¼ãã¼ãå¿
è¦ã«ãªãã¾ã。Firebase Node.js Server SDK 㨠Express ã¦ã§ããµã¼ãã¼ã使ãã¨、ã·ã³ãã«ãªãµã¼ãã¼ãæ§ç¯ã§ãã¾ã。
ã¾ã、ãµã¼ãã¼ã¯ã¦ã¼ã¶ã¼ã®ç«¯æ«ãã LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãåä¿¡ã、LINE Social Rest API ã使ã£ã¦ãããæ¤è¨¼ãã¾ã。API ã®ã¬ã¹ãã³ã¹ã§å¿ããã« channelId ã®å¤ã確èªã、ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãã¢ããªããçºè¡ããã¦ãããã¨ãæ¤è¨¼ãã¦ãã ãã。ããã¯ä»ã®ã¢ããªããã£ã³ãã«ã®ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãåå©ç¨ãã¦ã¢ããªã«ãã°ã¤ã³ãããã¨ãããªããã¾ãæ»æãé²ãããã§ã。
ãµã¼ãã¼(Node.js)
app.post('/verifyToken', (req, res) => {
if (!req.body.token) {
return res.status(400).send('Access Token not found');
}
const reqToken = req.body.token;
// ã¢ã¯ã»ã¹ ãã¼ã¯ã³ãæ¤è¨¼ããããã« LINE ãµã¼ãã¼ã«ãªã¯ã¨ã¹ããéä¿¡
const options = {
url: 'https://api.line.me/v1/oauth/verify',
headers: {
'Authorization': `Bearer ${reqToken}`
}
};
request(options, (error, response, body) => {
if (!error && response.statusCode === 200) {
const lineObj = JSON.parse(body);
// ãªããã¾ãæ»æãé²ãããã«å¿
ããã¼ã¯ã³ã® channelId ã確èª
if ((typeof lineObj.mid !== 'undefined')
&& (lineObj.channelId === myLINEChannelId)) {
// LINE ãµã¼ãã¼ã«ããã¢ã¯ã»ã¹ ãã¼ã¯ã³ã®æ¤è¨¼ãæå
// Firebase ãã¼ã¯ã³ãçæã、端æ«ã«è¿å´
const firebaseToken = generateFirebaseToken(lineObj.mid);
// LINE ãããã¡ã¤ã«ã§ Firebase ã¦ã¼ã¶ã¼ ãããã¡ã¤ã«ãã¢ãããã¼ã
updateUserProfile(reqToken, firebaseToken, lineObj.mid, () => {
const ret = {
firebase_token: firebaseToken
};
return res.status(200).send(ret);
});
}
}
const ret = {
error_message:'Authentication error:Cannot verify access token.'
};
return res.status(403).send(ret);
});
}
});
LINE ã¢ã¯ã»ã¹ ãã¼ã¯ã³ã®æ¤è¨¼ãæåããã、Firebase Server SDK ã使ã£ã¦ Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãçæã、ã¦ã¼ã¶ã¼ã®ç«¯æ«ã«è¿ãã¾ã。LINE ã®ã¦ã¼ã¶ã¼ ID ã¯、Firebase ã®ã¦ã¼ã¶ã¼ ID ã¨ãã¦åå©ç¨ã§ãã¾ã。
ãµã¼ãã¼(Node.js)
function generateFirebaseToken(lineMid) {
var firebaseUid = 'line:' + lineMid;
var additionalClaims = {
provider:'LINE'
};
return firebase.auth().createCustomToken(firebaseUid);
}
Firebase Server Java SDK ã使ããã¨ãã§ãã¾ã。
App Engine Flexible Environment ã Cloud Functions ã®ãããªã½ãªã¥ã¼ã·ã§ã³ã使ãã¨、ãµã¼ãã¼ã管çããå¿
è¦ã¯ãªããªãã¾ã。
ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ã«ãã Firebase ã¸ã®ãã°ã¤ã³
Firebase ã«ã¹ã¿ã èªè¨¼ãã¼ã¯ã³ãåä¿¡ããã、ããã使ã£ã¦ Firebase ã«ãã°ã¤ã³ãã¾ã。
iOS(Objective-C)
[[FIRAuth auth] signInWithCustomToken:firebaseToken
completion:^(FIRUser * _Nullable user, NSError * _Nullable error) {
// ãã°ã¤ã³çµæã®å¦ç
// ・・・
}];
Android
FirebaseAuth.getInstance()
.signInWithCustomToken(firebaseToken)
.addOnCompleteListener(new OnCompleteListener() {
// ãã°ã¤ã³çµæã®å¦ç
// ・・・
});
試ãã¦ã¿ã
ãµã³ãã«ã³ã¼ãã¯ãªã¼ãã³ã½ã¼ã¹ã§ã。ãã²ãã¦ã³ãã¼ããã¦ã試ããã ãã。 https://github.com/firebase/custom-auth-samples/tree/master/Line
Posted by Khanh LeViet - Developer Relations Team