Skip to content

Commit 4e4dc95

Browse files
authored
Merge pull request #3 from MuShare/develop/pluto-master
Fix refreshing token
2 parents 82fa238 + 35ef0c6 commit 4e4dc95

File tree

11 files changed

+127
-25
lines changed

11 files changed

+127
-25
lines changed

.idea/codeStyles/Project.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demoapp/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<category android:name="android.intent.category.LAUNCHER" />
2121
</intent-filter>
2222
</activity>
23+
<activity android:name=".ui.login.ProfileActivity">
24+
<meta-data
25+
android:name="android.support.PARENT_ACTIVITY"
26+
android:value=".ui.login.LoginActivity" />
27+
</activity>
2328
</application>
2429

2530
</manifest>

demoapp/src/main/java/com/mushare/demoapp/ui/login/LoginActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mushare.demoapp.ui.login
22

33
import android.app.Activity
4+
import android.content.Intent
45
import androidx.lifecycle.Observer
56
import androidx.lifecycle.ViewModelProviders
67
import android.os.Bundle
@@ -102,6 +103,10 @@ class LoginActivity : AppCompatActivity() {
102103
loginViewModel.login(username.text.toString(), password.text.toString())
103104
}
104105
}
106+
107+
if (Pluto.getInstance()?.state == Pluto.State.signin) {
108+
startActivity(Intent(this, ProfileActivity::class.java))
109+
}
105110
}
106111

107112
private fun updateUiWithUser(model: LoggedInUserView) {
@@ -113,6 +118,10 @@ class LoginActivity : AppCompatActivity() {
113118
"$welcome $displayName",
114119
Toast.LENGTH_LONG
115120
).show()
121+
122+
val intent = Intent(this, ProfileActivity::class.java)
123+
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
124+
startActivity(intent)
116125
}
117126

118127
private fun showLoginFailed(@StringRes errorString: Int) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.mushare.demoapp.ui.login;
2+
3+
import android.os.Bundle
4+
import android.widget.Button
5+
import android.widget.TextView
6+
import android.widget.Toast
7+
import androidx.appcompat.app.AppCompatActivity
8+
import com.mushare.demoapp.R
9+
import com.mushare.plutosdk.Pluto
10+
import com.mushare.plutosdk.getToken
11+
import com.mushare.plutosdk.myInfo
12+
13+
class ProfileActivity : AppCompatActivity() {
14+
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
18+
setContentView(R.layout.activity_profile)
19+
20+
Pluto.getInstance()?.myInfo(success = {
21+
findViewById<TextView>(R.id.profile_name).text = it.name
22+
})
23+
24+
Pluto.getInstance()?.getToken(completion = {
25+
findViewById<TextView>(R.id.profile_access_token).text = it ?: "Refresh failed"
26+
})
27+
28+
findViewById<Button>(R.id.profile_refresh_token).setOnClickListener {
29+
Pluto.getInstance()?.getToken(isForceRefresh = true, completion = {
30+
findViewById<TextView>(R.id.profile_access_token).text = it ?: "Refresh failed"
31+
})
32+
}
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
xmlns:app="http://schemas.android.com/apk/res-auto">
6+
7+
<TextView
8+
android:id="@+id/profile_name"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
app:layout_constraintTop_toTopOf="parent"
12+
android:layout_marginStart="20dp"
13+
android:layout_marginTop="20dp"
14+
android:layout_marginEnd="20dp"
15+
android:text="Username"/>
16+
17+
<Button
18+
android:id="@+id/profile_refresh_token"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
app:layout_constraintTop_toBottomOf="@id/profile_name"
22+
android:layout_marginStart="20dp"
23+
android:layout_marginTop="20dp"
24+
android:layout_marginEnd="20dp"
25+
android:text="Refresh Token"/>
26+
27+
<TextView
28+
android:id="@+id/profile_access_token"
29+
android:layout_width="match_parent"
30+
android:layout_height="wrap_content"
31+
app:layout_constraintTop_toBottomOf="@id/profile_refresh_token"
32+
android:layout_marginStart="20dp"
33+
android:layout_marginTop="20dp"
34+
android:layout_marginEnd="20dp"/>
35+
36+
</androidx.constraintlayout.widget.ConstraintLayout>

pluto-kotlin-client-sdk/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: 'maven-publish'
55

66
buildscript {
77
ext.versionCode = 11
8-
ext.versionName = '0.2'
8+
ext.versionName = '0.2.1'
99
}
1010

1111
android {

pluto-kotlin-client-sdk/src/main/java/com/mushare/plutosdk/Pluto+Auth.kt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import retrofit2.Call
44
import retrofit2.Callback
55
import retrofit2.Response
66

7-
fun Pluto.getToken(completion: (String?) -> Unit, handler: Pluto.PlutoRequestHandler? = null) {
7+
fun Pluto.getToken(isForceRefresh: Boolean = false, completion: (String?) -> Unit, handler: Pluto.PlutoRequestHandler? = null) {
88
val jwt = data.jwt
99
val expire = data.expire
10-
if (jwt == null || expire == null || expire - System.currentTimeMillis() / 1000 < 5 * 60) {
10+
if (isForceRefresh || jwt == null || expire == null || expire - System.currentTimeMillis() / 1000 < 5 * 60) {
1111
refreshToken({
1212
if (it == null) {
1313
data.clear()
@@ -19,13 +19,12 @@ fun Pluto.getToken(completion: (String?) -> Unit, handler: Pluto.PlutoRequestHan
1919
}
2020

2121
private fun Pluto.refreshToken(completion: (String?) -> Unit, handler: Pluto.PlutoRequestHandler? = null) {
22-
val userId = data.userId
2322
val refreshToken = data.refreshToken
24-
if (userId == null || refreshToken == null) {
23+
if (refreshToken == null) {
2524
completion(null)
2625
return
2726
}
28-
plutoService.refreshAuth(RefreshAuthPostData(refreshToken, userId)).apply {
27+
plutoService.refreshAuth(RefreshAuthPostData(refreshToken, Pluto.appId)).apply {
2928
enqueue(object : Callback<PlutoResponseWithBody<RefreshAuthResponse>> {
3029
override fun onFailure(
3130
call: Call<PlutoResponseWithBody<RefreshAuthResponse>>,
@@ -38,9 +37,11 @@ private fun Pluto.refreshToken(completion: (String?) -> Unit, handler: Pluto.Plu
3837
override fun onResponse(call: Call<PlutoResponseWithBody<RefreshAuthResponse>>, response: Response<PlutoResponseWithBody<RefreshAuthResponse>>) {
3938
val plutoResponse = response.body()
4039
if (plutoResponse != null && plutoResponse.statusOK()) {
41-
val jwt = plutoResponse.getBody().jwt
42-
if (data.updateJwt(jwt)) {
43-
completion(jwt)
40+
val accessToken = plutoResponse.getBody().accessToken
41+
val refreshToken = plutoResponse.getBody().refreshToken
42+
if (data.updateJwt(accessToken)) {
43+
data.updateRefreshToken(refreshToken)
44+
completion(accessToken)
4445
} else {
4546
completion(null)
4647
}
@@ -55,11 +56,14 @@ private fun Pluto.refreshToken(completion: (String?) -> Unit, handler: Pluto.Plu
5556
}
5657

5758
fun Pluto.getAuthorizationHeader(completion: (Map<String, String>?) -> Unit, handler: Pluto.PlutoRequestHandler? = null) {
58-
getToken({ token ->
59-
if (token == null) {
60-
completion(null)
61-
} else {
62-
completion(hashMapOf("Authorization" to "jwt $token"))
63-
}
64-
}, handler)
59+
getToken(
60+
completion = { token ->
61+
if (token == null) {
62+
completion(null)
63+
} else {
64+
completion(hashMapOf("Authorization" to "jwt $token"))
65+
}
66+
},
67+
handler = handler
68+
)
6569
}

pluto-kotlin-client-sdk/src/main/java/com/mushare/plutosdk/Pluto.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Pluto private constructor() {
6060
}
6161

6262
init {
63-
getToken({
63+
getToken(completion = {
6464
state.postValue(
6565
if (it == null) {
6666
State.notSignin

pluto-kotlin-client-sdk/src/main/java/com/mushare/plutosdk/PlutoModel.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,16 @@ internal class PlutoModel(context: Context) {
4444
private var _userId = IntWrapper(USER_ID_SAVE_KEY, sharedPref)
4545
val userId get() = _userId.value
4646

47-
private var _mail = StringWrapper(MAIL_SAVE_KEY, sharedPref)
4847
private var _name = StringWrapper(NAME_SAVE_KEY, sharedPref)
4948
private var _avatar = StringWrapper(AVATAR_SAVE_KEY, sharedPref)
5049

5150
var user: PlutoUser?
5251
get() {
53-
return if (_userId.value == null || _mail.value == null || _name.value == null || _avatar.value == null) null
54-
else PlutoUser(_userId.value!!, _mail.value!!, _avatar.value!!, _name.value!!)
52+
return if (_userId.value == null || _name.value == null || _avatar.value == null) null
53+
else PlutoUser(_userId.value!!, _avatar.value!!, _name.value!!)
5554
}
5655
set(value) {
5756
_userId.value = value?.id
58-
_mail.value = value?.mail
5957
_name.value = value?.name
6058
_avatar.value = value?.avatar
6159
}
@@ -87,8 +85,7 @@ internal class PlutoModel(context: Context) {
8785
}
8886

8987
data class PlutoUser(
90-
@field:SerializedName("id") var id: Int,
91-
@field:SerializedName("mail") var mail: String,
88+
@field:SerializedName("sub") var id: Int,
9289
@field:SerializedName("avatar") var avatar: String,
9390
@field:SerializedName("name") var name: String
9491
)

pluto-kotlin-client-sdk/src/main/java/com/mushare/plutosdk/PlutoResponse.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class PlutoResponseErrorData(
2626
)
2727

2828
class RefreshAuthResponse(
29-
@field:SerializedName("jwt") var jwt: String
29+
@field:SerializedName("access_token") var accessToken: String,
30+
@field:SerializedName("refresh_token") var refreshToken: String
3031
)
3132

3233
class LoginResponse(

0 commit comments

Comments
 (0)