Skip to content

Commit fd689e6

Browse files
committed
add auto login & fix update custom data API
1 parent 76788d7 commit fd689e6

File tree

18 files changed

+539
-87
lines changed

18 files changed

+539
-87
lines changed

app/src/main/java/cn/authing/MainActivity.java

Lines changed: 100 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,43 @@
44

55
import android.content.Intent;
66
import android.os.Bundle;
7+
import android.view.Gravity;
8+
import android.view.View;
9+
import android.view.ViewGroup;
710
import android.widget.Button;
11+
import android.widget.LinearLayout;
12+
import android.widget.Space;
813
import android.widget.TextView;
914

15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
1019
import cn.authing.guard.Authing;
20+
import cn.authing.guard.activity.UpdateCustomDataActivity;
1121
import cn.authing.guard.data.UserInfo;
1222
import cn.authing.guard.util.Util;
1323

1424
public class MainActivity extends AppCompatActivity {
1525

26+
private TextView tvNickName;
27+
private TextView tvName;
28+
private TextView tvUserName;
29+
private TextView tvPhone;
30+
private TextView tvEmail;
31+
32+
private Map<String, TextView> customDataViews = new HashMap<>();
33+
1634
@Override
1735
protected void onCreate(Bundle savedInstanceState) {
1836
super.onCreate(savedInstanceState);
1937
setContentView(R.layout.activity_main);
2038

21-
UserInfo userInfo = (UserInfo) getIntent().getSerializableExtra("user");
22-
TextView tv = findViewById(R.id.tv_nick_name);
23-
setText(tv, userInfo.getNickname());
24-
25-
tv = findViewById(R.id.tv_name);
26-
setText(tv, userInfo.getName());
27-
28-
tv = findViewById(R.id.tv_username);
29-
setText(tv, userInfo.getUsername());
30-
31-
tv = findViewById(R.id.tv_phone);
32-
setText(tv, userInfo.getPhone_number());
33-
34-
tv = findViewById(R.id.tv_email);
35-
setText(tv, userInfo.getEmail());
36-
39+
tvNickName = findViewById(R.id.tv_nick_name);
40+
tvName = findViewById(R.id.tv_name);
41+
tvUserName = findViewById(R.id.tv_username);
42+
tvPhone = findViewById(R.id.tv_phone);
43+
tvEmail = findViewById(R.id.tv_email);
3744

3845
TextView tvChangePassword = findViewById(R.id.tv_change_password);
3946
tvChangePassword.setOnClickListener((v)->{
@@ -42,17 +49,82 @@ protected void onCreate(Bundle savedInstanceState) {
4249
finish();
4350
});
4451

52+
UserInfo userInfo = Authing.getCurrentUser();
53+
if (userInfo == null) {
54+
return;
55+
}
56+
LinearLayout customData = findViewById(R.id.ll_custom_data);
57+
setupCustomDataUI(customData, userInfo);
58+
4559
Button btn = findViewById(R.id.btn_logout);
4660
btn.setOnClickListener(v -> logout());
4761
}
4862

63+
@Override
64+
protected void onResume() {
65+
super.onResume();
66+
UserInfo userInfo = Authing.getCurrentUser();
67+
if (userInfo == null) {
68+
return;
69+
}
70+
71+
setText(tvNickName, userInfo.getNickname());
72+
setText(tvName, userInfo.getName());
73+
setText(tvUserName, userInfo.getUsername());
74+
setText(tvPhone, userInfo.getPhone_number());
75+
setText(tvEmail, userInfo.getEmail());
76+
77+
for (UserInfo.CustomData data : userInfo.getCustomData()) {
78+
TextView tv = customDataViews.get(data.getKey());
79+
tv.setText(data.getValue());
80+
}
81+
}
82+
83+
private void setupCustomDataUI(LinearLayout container, UserInfo user) {
84+
int padding = (int)getResources().getDimension(R.dimen.authing_form_start_end_margin);
85+
for (UserInfo.CustomData data : user.getCustomData()) {
86+
LinearLayout layout = new LinearLayout(this);
87+
layout.setOrientation(LinearLayout.HORIZONTAL);
88+
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)Util.dp2px(this, 48));
89+
layout.setLayoutParams(lp);
90+
layout.setPadding(padding, 0, padding, 0);
91+
layout.setGravity(Gravity.CENTER_VERTICAL);
92+
container.addView(layout);
93+
94+
TextView tvLabel = new TextView(this);
95+
tvLabel.setText(data.getLabel());
96+
tvLabel.setTextSize(16);
97+
layout.addView(tvLabel);
98+
99+
Space space = new Space(this);
100+
LinearLayout.LayoutParams lpSpace = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
101+
space.setLayoutParams(lpSpace);
102+
layout.addView(space);
103+
104+
TextView tvValue = new TextView(this);
105+
tvValue.setTextSize(16);
106+
layout.addView(tvValue);
107+
customDataViews.put(data.getKey(), tvValue);
108+
109+
View sep = new View(this);
110+
LinearLayout.LayoutParams lpSep = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
111+
int m = (int)getResources().getDimension(R.dimen.authing_form_start_end_margin);
112+
lpSep.setMargins(m, 0, 0, 0);
113+
sep.setBackgroundColor(0xffdddddd);
114+
sep.setLayoutParams(lpSep);
115+
container.addView(sep);
116+
117+
layout.setOnClickListener((v -> {
118+
goUpdateUserData(data);
119+
}));
120+
}
121+
}
122+
49123
private void logout() {
50-
Authing.logout((ok, data)->{
51-
if (ok) {
52-
Intent intent = new Intent(this, SampleListActivity.class);
53-
startActivity(intent);
54-
finish();
55-
}
124+
Authing.logout((code, message, data)->{
125+
Intent intent = new Intent(this, SampleListActivity.class);
126+
startActivity(intent);
127+
finish();
56128
});
57129
}
58130

@@ -63,4 +135,10 @@ private void setText(TextView tv, String s) {
63135
tv.setText(s);
64136
}
65137
}
138+
139+
private void goUpdateUserData(UserInfo.CustomData data) {
140+
Intent intent = new Intent(this, UpdateCustomDataActivity.class);
141+
intent.putExtra("data", data);
142+
startActivity(intent);
143+
}
66144
}

app/src/main/java/cn/authing/SplashActivity.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,39 @@
88

99
import java.util.Objects;
1010

11+
import cn.authing.guard.Authing;
12+
import cn.authing.guard.data.Safe;
13+
1114
public class SplashActivity extends AppCompatActivity {
1215

16+
private int flag;
17+
1318
@Override
1419
protected void onCreate(Bundle savedInstanceState) {
1520
super.onCreate(savedInstanceState);
1621

1722
Objects.requireNonNull(getSupportActionBar()).hide();
1823
setContentView(R.layout.activity_splash);
1924

20-
new Handler().postDelayed(this::gotoLogin, 1000);
25+
new Handler().postDelayed(()-> next(1), 1000);
26+
27+
Authing.autoLogin(((code, message, userInfo) -> next(2)));
2128
}
2229

23-
private void gotoLogin() {
24-
Intent intent = new Intent(this, SampleListActivity.class);
25-
startActivity(intent);
26-
finish();
30+
private void next(int f) {
31+
flag |= f;
32+
33+
// both condition meets
34+
if (flag == 3) {
35+
Intent intent;
36+
if (Authing.getCurrentUser() != null) {
37+
intent = new Intent(this, MainActivity.class);
38+
intent.putExtra("user", Authing.getCurrentUser());
39+
} else {
40+
intent = new Intent(this, SampleListActivity.class);
41+
}
42+
startActivity(intent);
43+
finish();
44+
}
2745
}
28-
}
46+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
android:id="@+id/tv_welcome"
1616
android:layout_width="wrap_content"
1717
android:layout_height="48dp"
18-
app:layout_constraintLeft_toLeftOf="parent"
19-
app:layout_constraintRight_toRightOf="parent"
20-
app:layout_constraintTop_toBottomOf="@id/tv_name"
2118
android:textColor="@color/black"
2219
android:textSize="18sp"
20+
android:gravity="center_vertical"
2321
android:layout_gravity="center_horizontal"
24-
android:paddingStart="16dp"
25-
android:text="欢迎来到 Authing Demo App 主页"/>
22+
android:text="个人中心"/>
23+
24+
<TextView
25+
android:layout_width="match_parent"
26+
android:layout_height="48dp"
27+
android:gravity="center_vertical"
28+
android:textColor="@color/black"
29+
android:paddingStart="@dimen/authing_form_start_end_margin"
30+
android:textSize="18sp"
31+
android:text="基本信息"/>
2632

2733
<LinearLayout
2834
android:paddingStart="@dimen/authing_form_start_end_margin"
@@ -184,15 +190,33 @@
184190
android:layout_marginStart="@dimen/authing_form_start_end_margin"
185191
android:background="#DDD" />
186192

193+
<TextView
194+
android:layout_width="match_parent"
195+
android:layout_height="48dp"
196+
android:layout_marginTop="16dp"
197+
android:gravity="center_vertical"
198+
android:textColor="@color/black"
199+
android:paddingStart="@dimen/authing_form_start_end_margin"
200+
android:textSize="18sp"
201+
android:text="扩展属性"/>
202+
203+
<LinearLayout
204+
android:id="@+id/ll_custom_data"
205+
android:orientation="vertical"
206+
android:layout_width="match_parent"
207+
android:layout_height="wrap_content">
208+
209+
</LinearLayout>
210+
187211
<Button
188212
android:id="@+id/btn_logout"
189213
android:layout_width="match_parent"
190214
android:layout_height="wrap_content"
191215
android:layout_marginStart="@dimen/authing_form_start_end_margin"
192216
android:layout_marginEnd="@dimen/authing_form_start_end_margin"
193-
android:background="@drawable/authing_button_background"
217+
android:backgroundTint="#EEE"
194218
android:layout_marginTop="32dp"
195-
android:textColor="@color/white"
196-
android:text="登出"/>
219+
android:textColor="#FF3B30"
220+
android:text="@string/authing_logout"/>
197221
</LinearLayout>
198222
</ScrollView>

doc/social/alipay.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,62 @@
1-
# 建设中
1+
# 集成支付宝步骤
2+
3+
1. 在这个页面下载 [支付宝 Android SDK](https://opendocs.alipay.com/open/54/104509)
4+
5+
>支付宝将 Android、iOS 的 SDK 和 Demo 打包到一个 zip 包里面,找到里面的安卓 SDK,拷贝到 app 的 libs 目录
6+
7+
2. 设置依赖:
8+
```groovy
9+
implementation 'cn.authing:guard:+'
10+
implementation files('libs/alipaysdk.aar')
11+
```
12+
13+
>Guard 只是 compileOnly 依赖微信,这样可以让 App 按需引入,防止 Guard aar 包随着支持的第三方登录增加而越来越大。所以每增加一个第三方身份源,都需要 App 手动加上该身份源的依赖
14+
15+
3. 在应用启动的时候设置支付宝 AppID:
16+
```java
17+
Alipay.appId = "2021002192647456";
18+
```
19+
20+
4. 在应用启动的时候初始化 Authing:
21+
```java
22+
// appId 是 authing 的应用 id,可以在 authing 控制台里面获取
23+
Authing.init(context, appId);
24+
```
25+
26+
接下来,如果使用我们提供的支付宝登录按钮,则在布局文件里面加上(当然也可以用代码初始化):
27+
28+
```xml
29+
<cn.authing.guard.AlipayLoginButton
30+
android:id="@+id/btn_alipay_login"
31+
android:layout_width="44dp"
32+
android:layout_height="44dp"
33+
app:layout_constraintLeft_toLeftOf="parent"
34+
app:layout_constraintRight_toRightOf="parent"/>
35+
```
36+
37+
然后在 java 代码里面处理事件:
38+
39+
```java
40+
AlipayLoginButton button = findViewById(R.id.btn_alipay_login);
41+
button.setOnLoginListener((ok, data) -> {
42+
if (ok) {
43+
// 登录成功,data 是用户信息
44+
} else {
45+
// 登录失败
46+
}
47+
});
48+
```
49+
50+
<br>
51+
52+
如果不想使用我们内置的按钮,则可以在自己按钮的点击事件里面调用:
53+
54+
```java
55+
Alipay.login(appContext, ((ok, data) -> {
56+
if (ok) {
57+
// 登录成功,data 是用户信息
58+
} else {
59+
// 登录失败
60+
}
61+
}));
62+
```

guard/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GROUP=cn.authing
22
POM_ARTIFACT_ID=guard
3-
VERSION_NAME=1.1.1
3+
VERSION_NAME=1.1.2
44

55
POM_NAME=guard
66
POM_PACKAGING=aar

guard/src/main/AndroidManifest.xml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
4-
package="cn.authing.guard" >
4+
package="cn.authing.guard">
55

66
<uses-permission android:name="android.permission.INTERNET" />
77
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
@@ -10,10 +10,13 @@
1010
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1111

1212
<application>
13+
<activity
14+
android:name=".activity.UpdateCustomDataActivity"
15+
android:exported="false" />
1316
<activity
1417
android:name=".activity.BaseLoginActivity"
15-
android:exported="false" >
16-
<intent-filter tools:node="replace" >
18+
android:exported="false">
19+
<intent-filter tools:node="replace">
1720
<category android:name="android.intent.category.DEFAULT" />
1821
<category android:name="android.intent.category.BROWSABLE" />
1922

@@ -22,8 +25,8 @@
2225
</activity>
2326
<activity
2427
android:name=".activity.AuthActivity"
25-
android:exported="false" >
26-
<intent-filter tools:node="replace" >
28+
android:exported="false">
29+
<intent-filter tools:node="replace">
2730
<category android:name="android.intent.category.DEFAULT" />
2831
<category android:name="android.intent.category.BROWSABLE" />
2932

@@ -32,8 +35,8 @@
3235
</activity>
3336
<activity
3437
android:name=".activity.IndexAuthActivity"
35-
android:launchMode="singleTask"
3638
android:exported="false"
39+
android:launchMode="singleTask"
3740
android:screenOrientation="portrait" />
3841
</application>
3942

0 commit comments

Comments
 (0)