Skip to content

Commit

Permalink
add auto login & fix update custom data API
Browse files Browse the repository at this point in the history
  • Loading branch information
lancemao committed Jan 6, 2022
1 parent 76788d7 commit fd689e6
Show file tree
Hide file tree
Showing 18 changed files with 539 additions and 87 deletions.
122 changes: 100 additions & 22 deletions app/src/main/java/cn/authing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,43 @@

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.authing.guard.Authing;
import cn.authing.guard.activity.UpdateCustomDataActivity;
import cn.authing.guard.data.UserInfo;
import cn.authing.guard.util.Util;

public class MainActivity extends AppCompatActivity {

private TextView tvNickName;
private TextView tvName;
private TextView tvUserName;
private TextView tvPhone;
private TextView tvEmail;

private Map<String, TextView> customDataViews = new HashMap<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

UserInfo userInfo = (UserInfo) getIntent().getSerializableExtra("user");
TextView tv = findViewById(R.id.tv_nick_name);
setText(tv, userInfo.getNickname());

tv = findViewById(R.id.tv_name);
setText(tv, userInfo.getName());

tv = findViewById(R.id.tv_username);
setText(tv, userInfo.getUsername());

tv = findViewById(R.id.tv_phone);
setText(tv, userInfo.getPhone_number());

tv = findViewById(R.id.tv_email);
setText(tv, userInfo.getEmail());

tvNickName = findViewById(R.id.tv_nick_name);
tvName = findViewById(R.id.tv_name);
tvUserName = findViewById(R.id.tv_username);
tvPhone = findViewById(R.id.tv_phone);
tvEmail = findViewById(R.id.tv_email);

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

UserInfo userInfo = Authing.getCurrentUser();
if (userInfo == null) {
return;
}
LinearLayout customData = findViewById(R.id.ll_custom_data);
setupCustomDataUI(customData, userInfo);

Button btn = findViewById(R.id.btn_logout);
btn.setOnClickListener(v -> logout());
}

@Override
protected void onResume() {
super.onResume();
UserInfo userInfo = Authing.getCurrentUser();
if (userInfo == null) {
return;
}

setText(tvNickName, userInfo.getNickname());
setText(tvName, userInfo.getName());
setText(tvUserName, userInfo.getUsername());
setText(tvPhone, userInfo.getPhone_number());
setText(tvEmail, userInfo.getEmail());

for (UserInfo.CustomData data : userInfo.getCustomData()) {
TextView tv = customDataViews.get(data.getKey());
tv.setText(data.getValue());
}
}

private void setupCustomDataUI(LinearLayout container, UserInfo user) {
int padding = (int)getResources().getDimension(R.dimen.authing_form_start_end_margin);
for (UserInfo.CustomData data : user.getCustomData()) {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)Util.dp2px(this, 48));
layout.setLayoutParams(lp);
layout.setPadding(padding, 0, padding, 0);
layout.setGravity(Gravity.CENTER_VERTICAL);
container.addView(layout);

TextView tvLabel = new TextView(this);
tvLabel.setText(data.getLabel());
tvLabel.setTextSize(16);
layout.addView(tvLabel);

Space space = new Space(this);
LinearLayout.LayoutParams lpSpace = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
space.setLayoutParams(lpSpace);
layout.addView(space);

TextView tvValue = new TextView(this);
tvValue.setTextSize(16);
layout.addView(tvValue);
customDataViews.put(data.getKey(), tvValue);

View sep = new View(this);
LinearLayout.LayoutParams lpSep = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
int m = (int)getResources().getDimension(R.dimen.authing_form_start_end_margin);
lpSep.setMargins(m, 0, 0, 0);
sep.setBackgroundColor(0xffdddddd);
sep.setLayoutParams(lpSep);
container.addView(sep);

layout.setOnClickListener((v -> {
goUpdateUserData(data);
}));
}
}

private void logout() {
Authing.logout((ok, data)->{
if (ok) {
Intent intent = new Intent(this, SampleListActivity.class);
startActivity(intent);
finish();
}
Authing.logout((code, message, data)->{
Intent intent = new Intent(this, SampleListActivity.class);
startActivity(intent);
finish();
});
}

Expand All @@ -63,4 +135,10 @@ private void setText(TextView tv, String s) {
tv.setText(s);
}
}

private void goUpdateUserData(UserInfo.CustomData data) {
Intent intent = new Intent(this, UpdateCustomDataActivity.class);
intent.putExtra("data", data);
startActivity(intent);
}
}
30 changes: 24 additions & 6 deletions app/src/main/java/cn/authing/SplashActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,39 @@

import java.util.Objects;

import cn.authing.guard.Authing;
import cn.authing.guard.data.Safe;

public class SplashActivity extends AppCompatActivity {

private int flag;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

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

new Handler().postDelayed(this::gotoLogin, 1000);
new Handler().postDelayed(()-> next(1), 1000);

Authing.autoLogin(((code, message, userInfo) -> next(2)));
}

private void gotoLogin() {
Intent intent = new Intent(this, SampleListActivity.class);
startActivity(intent);
finish();
private void next(int f) {
flag |= f;

// both condition meets
if (flag == 3) {
Intent intent;
if (Authing.getCurrentUser() != null) {
intent = new Intent(this, MainActivity.class);
intent.putExtra("user", Authing.getCurrentUser());
} else {
intent = new Intent(this, SampleListActivity.class);
}
startActivity(intent);
finish();
}
}
}
}
40 changes: 32 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
android:id="@+id/tv_welcome"
android:layout_width="wrap_content"
android:layout_height="48dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_name"
android:textColor="@color/black"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_gravity="center_horizontal"
android:paddingStart="16dp"
android:text="欢迎来到 Authing Demo App 主页"/>
android:text="个人中心"/>

<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:textColor="@color/black"
android:paddingStart="@dimen/authing_form_start_end_margin"
android:textSize="18sp"
android:text="基本信息"/>

<LinearLayout
android:paddingStart="@dimen/authing_form_start_end_margin"
Expand Down Expand Up @@ -184,15 +190,33 @@
android:layout_marginStart="@dimen/authing_form_start_end_margin"
android:background="#DDD" />

<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:gravity="center_vertical"
android:textColor="@color/black"
android:paddingStart="@dimen/authing_form_start_end_margin"
android:textSize="18sp"
android:text="扩展属性"/>

<LinearLayout
android:id="@+id/ll_custom_data"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</LinearLayout>

<Button
android:id="@+id/btn_logout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/authing_form_start_end_margin"
android:layout_marginEnd="@dimen/authing_form_start_end_margin"
android:background="@drawable/authing_button_background"
android:backgroundTint="#EEE"
android:layout_marginTop="32dp"
android:textColor="@color/white"
android:text="登出"/>
android:textColor="#FF3B30"
android:text="@string/authing_logout"/>
</LinearLayout>
</ScrollView>
63 changes: 62 additions & 1 deletion doc/social/alipay.md
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
# 建设中
# 集成支付宝步骤

1. 在这个页面下载 [支付宝 Android SDK](https://opendocs.alipay.com/open/54/104509)

>支付宝将 Android、iOS 的 SDK 和 Demo 打包到一个 zip 包里面,找到里面的安卓 SDK,拷贝到 app 的 libs 目录
2. 设置依赖:
```groovy
implementation 'cn.authing:guard:+'
implementation files('libs/alipaysdk.aar')
```

>Guard 只是 compileOnly 依赖微信,这样可以让 App 按需引入,防止 Guard aar 包随着支持的第三方登录增加而越来越大。所以每增加一个第三方身份源,都需要 App 手动加上该身份源的依赖
3. 在应用启动的时候设置支付宝 AppID:
```java
Alipay.appId = "2021002192647456";
```

4. 在应用启动的时候初始化 Authing:
```java
// appId 是 authing 的应用 id,可以在 authing 控制台里面获取
Authing.init(context, appId);
```

接下来,如果使用我们提供的支付宝登录按钮,则在布局文件里面加上(当然也可以用代码初始化):

```xml
<cn.authing.guard.AlipayLoginButton
android:id="@+id/btn_alipay_login"
android:layout_width="44dp"
android:layout_height="44dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
```

然后在 java 代码里面处理事件:

```java
AlipayLoginButton button = findViewById(R.id.btn_alipay_login);
button.setOnLoginListener((ok, data) -> {
if (ok) {
// 登录成功,data 是用户信息
} else {
// 登录失败
}
});
```

<br>

如果不想使用我们内置的按钮,则可以在自己按钮的点击事件里面调用:

```java
Alipay.login(appContext, ((ok, data) -> {
if (ok) {
// 登录成功,data 是用户信息
} else {
// 登录失败
}
}));
```
2 changes: 1 addition & 1 deletion guard/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GROUP=cn.authing
POM_ARTIFACT_ID=guard
VERSION_NAME=1.1.1
VERSION_NAME=1.1.2

POM_NAME=guard
POM_PACKAGING=aar
Expand Down
15 changes: 9 additions & 6 deletions guard/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="cn.authing.guard" >
package="cn.authing.guard">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand All @@ -10,10 +10,13 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application>
<activity
android:name=".activity.UpdateCustomDataActivity"
android:exported="false" />
<activity
android:name=".activity.BaseLoginActivity"
android:exported="false" >
<intent-filter tools:node="replace" >
android:exported="false">
<intent-filter tools:node="replace">
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

Expand All @@ -22,8 +25,8 @@
</activity>
<activity
android:name=".activity.AuthActivity"
android:exported="false" >
<intent-filter tools:node="replace" >
android:exported="false">
<intent-filter tools:node="replace">
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

Expand All @@ -32,8 +35,8 @@
</activity>
<activity
android:name=".activity.IndexAuthActivity"
android:launchMode="singleTask"
android:exported="false"
android:launchMode="singleTask"
android:screenOrientation="portrait" />
</application>

Expand Down
Loading

0 comments on commit fd689e6

Please sign in to comment.