const Authing = require('authing-js-sdk');
// 初始化 Authing SDK for JavaScript
const authing = new Authing({
userPoolId: 'your_userpool_id',
});
// 调用 APP 扫码登录的方法,此方法将生成一个用于扫码登录的图片和相关提示信息
authing.qrlogin.startScanning({
onSuccess(data) {
alert('扫码成功,请打开控制台查看用户信息')
console.log(data);
const {ticket, userInfo} = data;
// 存储 token 到 localStorage 中
localStorage.setItem('token', userInfo.token);
}
})
扫码成功之后,Authing 将会回调开发者传入的 onSuccess,回调的参数中包含了 ticket 和 userInfo,ticket 可以用来换取用户信息。
{
"scene": "APP_AUTH",
"qrcodeId": "5e05f0c57fde537d950f7da5",
"userPoolId": "5e04ae0d5f3cee22fb37612b",
"createdAt": "2019-12-27T11:53:41.260Z",
"expireAt": "2019-12-27T11:55:41.260Z",
"userDefinedData": { "hello": "world" }
}
要实现 APP 扫描登录 Web 端,首先要求 APP 端用户处于登录状态(这是理所当然的),调用相关接口的时候要带上终端用户的 token。移动端一共需要用到三个接口:
- (void) ConfirmAuthorization:(NSString *) qrcodeId{
NSURL * api =[NSURL URLWithString:@"http://oauth.authing.cn/oauth/scan-qrcode/confirm"];
NSDictionary *bodyDict = @{
@"qrcodeId": qrcodeId,
};
NSData *body = [NSJSONSerialization dataWithJSONObject:bodyDict options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:api];
request.HTTPMethod = @"POST";
[request setValue:self.USER_TOKEN forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:body];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if(httpResponse.statusCode == 200)
{
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"The response is - %@",responseDictionary);
NSInteger code = [[responseDictionary objectForKey:@"code"] integerValue];
if(code == 200)
{
NSLog(@"SUCCESS");
}
else
{
NSLog(@"FAILURE");
}
}
else
{
NSLog(@"Network Error");
}
}];
[dataTask resume];
}
authing.exchangeUserInfoWithTicket(ticket).then(res => {
const { code } = res
if (code === 200) {
console.log("Exchange userInfo success: ", res)
} else {
console.log("Exchange userInfo failed: ", res)
}
})
{
"code":200,
"message":"换取用户信息成功",
"data":{
"_id":"5e05bbf2d51b3761d5c71070",
"email":"983132@qq.com",
"emailVerified":false,
"oauth":"",
"registerMethod":"default:username-password",
"username":"983132@qq.com",
"nickname":"",
"company":"",
"photo":"https://usercontents.authing.co/authing-avatar.png",
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImVtYWlsIjoiOTgzMTMyQHFxLmNvbSIsImlxxxxxxxxx",
"phone":"",
"tokenExpiredAt":"2020-01-11T08:08:18.000Z",
"loginsCount":1,
"lastIP":"::1",
"signedUp":"2019-12-27T08:08:18.115Z",
"blocked":false,
"isDeleted":false
}
}