-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Refactor/#100 login api
- Loading branch information
Showing
31 changed files
with
530 additions
and
54 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/puzzling/puzzlingaos/data/datasource/local/UserDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.puzzling.puzzlingaos.data.datasource.local | ||
|
||
import androidx.datastore.core.Serializer | ||
import com.puzzling.puzzlingaos.data.entity.User | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.serialization.json.Json | ||
import org.apache.commons.lang3.SerializationException | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import javax.inject.Inject | ||
|
||
class UserDataSource @Inject constructor() : Serializer<User> { | ||
override val defaultValue: User | ||
get() = User() | ||
|
||
override suspend fun readFrom(input: InputStream): User { | ||
return try { | ||
Json.decodeFromString( | ||
deserializer = User.serializer(), | ||
string = input.readBytes().decodeToString(), | ||
) | ||
} catch (e: SerializationException) { | ||
e.printStackTrace() | ||
defaultValue | ||
} | ||
} | ||
|
||
override suspend fun writeTo(t: User, output: OutputStream) { | ||
withContext(Dispatchers.IO) { | ||
output.write( | ||
Json.encodeToString( | ||
serializer = User.serializer(), | ||
value = t, | ||
).encodeToByteArray(), | ||
) | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/puzzling/puzzlingaos/data/datasource/remote/AuthDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.puzzling.puzzlingaos.data.datasource.remote | ||
|
||
import com.puzzling.puzzlingaos.data.model.response.ResponseLoginDto | ||
import com.puzzling.puzzlingaos.data.model.response.ResponseTokenDto | ||
|
||
interface AuthDataSource { | ||
suspend fun login(socialPlatform: String): ResponseLoginDto | ||
|
||
suspend fun getToken(): ResponseTokenDto | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/puzzling/puzzlingaos/data/datasource/remote/impl/AuthDataSourceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.puzzling.puzzlingaos.data.datasource.remote.impl | ||
|
||
import com.puzzling.puzzlingaos.data.datasource.remote.AuthDataSource | ||
import com.puzzling.puzzlingaos.data.model.request.RequestLoginDto | ||
import com.puzzling.puzzlingaos.data.model.response.ResponseLoginDto | ||
import com.puzzling.puzzlingaos.data.model.response.ResponseTokenDto | ||
import com.puzzling.puzzlingaos.data.service.AuthService | ||
import com.puzzling.puzzlingaos.data.service.ReIssueTokenService | ||
import javax.inject.Inject | ||
|
||
class AuthDataSourceImpl @Inject constructor( | ||
private val authService: AuthService, | ||
private val reIssueTokenService: ReIssueTokenService, | ||
) : AuthDataSource { | ||
|
||
override suspend fun login(socialPlatform: String): ResponseLoginDto = | ||
authService.login(RequestLoginDto(socialPlatform)) | ||
|
||
override suspend fun getToken(): ResponseTokenDto = | ||
reIssueTokenService.getToken() | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/puzzling/puzzlingaos/data/entity/CurrentProject.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.puzzling.puzzlingaos.data.entity | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class CurrentProject( | ||
val projectId: Int, | ||
val projectName: String, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/puzzling/puzzlingaos/data/entity/User.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.puzzling.puzzlingaos.data.entity | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class User( | ||
val memberId: Int? = null, | ||
val name: String? = null, | ||
) |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/puzzling/puzzlingaos/data/model/request/RequestLoginDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.puzzling.puzzlingaos.data.model.request | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RequestLoginDto( | ||
@SerializedName("socialPlatform") | ||
val socialPlatform: String, | ||
) |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/puzzling/puzzlingaos/data/model/response/ResponseLoginDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.puzzling.puzzlingaos.data.model.response | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ResponseLoginDto( | ||
@SerialName("status") | ||
val status: Int, | ||
@SerialName("success") | ||
val success: Boolean, | ||
@SerialName("message") | ||
val message: String, | ||
@SerialName("data") | ||
val data: LoginData, | ||
|
||
) { | ||
@Serializable | ||
data class LoginData( | ||
@SerialName("name") | ||
val name: String, | ||
@SerialName("memberId") | ||
val memberId: Int, | ||
@SerialName("projectId") | ||
val projectId: Int?, | ||
@SerialName("projectName") | ||
val projectName: String?, | ||
@SerialName("accessToken") | ||
val accessToken: String, | ||
@SerialName("refreshToken") | ||
val refreshToken: String, | ||
@SerialName("isNewUser") | ||
val isNewUser: Boolean, | ||
) | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/puzzling/puzzlingaos/data/model/response/ResponseTokenDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.puzzling.puzzlingaos.data.model.response | ||
|
||
import com.puzzling.puzzlingaos.data.entity.Token | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ResponseTokenDto( | ||
@SerialName("status") | ||
val status: Int, | ||
@SerialName("success") | ||
val success: Boolean, | ||
@SerialName("message") | ||
val message: String, | ||
@SerialName("date") | ||
val data: NewToken?, | ||
) { | ||
@Serializable | ||
data class NewToken( | ||
@SerialName("accessToken") | ||
val accessToken: String, | ||
|
||
) | ||
|
||
fun getToken() = Token(data?.accessToken) | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/puzzling/puzzlingaos/data/repository/AuthRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.puzzling.puzzlingaos.data.repository | ||
|
||
import android.util.Log | ||
import androidx.datastore.core.DataStore | ||
import com.puzzling.puzzlingaos.data.datasource.remote.AuthDataSource | ||
import com.puzzling.puzzlingaos.data.entity.Token | ||
import com.puzzling.puzzlingaos.data.entity.User | ||
import com.puzzling.puzzlingaos.data.model.response.ResponseLoginDto | ||
import com.puzzling.puzzlingaos.domain.repository.AuthRepository | ||
import kotlinx.coroutines.flow.first | ||
import javax.inject.Inject | ||
|
||
class AuthRepositoryImpl @Inject constructor( | ||
private val tokenDataStore: DataStore<Token>, | ||
private val userDataSource: DataStore<User>, | ||
private val authDataSource: AuthDataSource, | ||
) : AuthRepository { | ||
override suspend fun login(socialPlatform: String): Result<ResponseLoginDto> = runCatching { | ||
authDataSource.login(socialPlatform) | ||
}.onSuccess { result -> | ||
tokenDataStore.updateData { Token(result.data.accessToken, result.data.refreshToken) } | ||
userDataSource.updateData { User(result.data.memberId, result.data.name) } | ||
Log.d("AuthRepoImpl", "$result") | ||
Log.d("AuthRepoImpl", "AuthRepoImpl get 토큰 ${userDataSource.data.first()}") | ||
}.onFailure { | ||
Log.d("AuthRepoImpl", "$it 에러") | ||
} | ||
|
||
override suspend fun getToken(): Result<Token> = runCatching { | ||
authDataSource.getToken().getToken() | ||
}.onSuccess { token -> | ||
tokenDataStore.updateData { Token(token.accessToken, token.refreshToken) } | ||
Log.d("AuthRepoImpl", "${tokenDataStore.data.first()}") | ||
}.onFailure { | ||
Log.d("AuthRepoImpl", "$it 에러") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/puzzling/puzzlingaos/data/repository/UserRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.puzzling.puzzlingaos.data.repository | ||
|
||
import androidx.datastore.core.DataStore | ||
import com.puzzling.puzzlingaos.data.entity.User | ||
import com.puzzling.puzzlingaos.domain.repository.UserRepository | ||
import kotlinx.coroutines.flow.first | ||
import javax.inject.Inject | ||
|
||
class UserRepositoryImpl @Inject constructor(private val dataStore: DataStore<User>) : | ||
UserRepository { | ||
override suspend fun setUserInfo(userInfo: User) { | ||
dataStore.updateData { User(userInfo.memberId, userInfo.name) } | ||
} | ||
|
||
override suspend fun getUserInfo(): User = dataStore.data.first() | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/puzzling/puzzlingaos/data/service/AuthService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.puzzling.puzzlingaos.data.service | ||
|
||
import com.puzzling.puzzlingaos.data.model.request.RequestLoginDto | ||
import com.puzzling.puzzlingaos.data.model.response.ResponseLoginDto | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
interface AuthService { | ||
@POST("api/v1/auth") | ||
suspend fun login( | ||
@Body socialPlatform: RequestLoginDto, | ||
): ResponseLoginDto | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/puzzling/puzzlingaos/data/service/ReIssueTokenService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.puzzling.puzzlingaos.data.service | ||
|
||
import com.puzzling.puzzlingaos.data.model.response.ResponseTokenDto | ||
import retrofit2.http.GET | ||
|
||
interface ReIssueTokenService { | ||
@GET("api/v1/auth/token") | ||
suspend fun getToken(): ResponseTokenDto | ||
} |
Oops, something went wrong.