add sign in option for android client on server

This commit is contained in:
Satindar Dhillon 2022-08-18 18:42:18 -07:00
parent 4bc1173de4
commit 1b46739a0d
8 changed files with 67 additions and 6 deletions

View file

@ -76,5 +76,36 @@ class LoginViewModel @Inject constructor(
Log.d(ContentValues.TAG, "Google Result?: $googleIdToken")
// TODO: submit id token to backend
// If token is missing then set the error message
if (googleIdToken == null) {
return
}
val login = RetrofitHelper.getInstance().create(AuthProviderLoginSubmit::class.java)
viewModelScope.launch {
isLoading = true
errorMessage = null
val result = login.submitAuthProviderLogin(
SignInParams(token = googleIdToken, provider = "GOOGLE")
)
isLoading = false
if (result.body()?.authToken != null) {
datastoreRepo.putString(DatastoreKeys.omnivoreAuthToken, result.body()?.authToken!!)
} else {
errorMessage = "Something went wrong. Please check your email/password and try again"
}
if (result.body()?.authCookieString != null) {
datastoreRepo.putString(
DatastoreKeys.omnivoreAuthCookieString, result.body()?.authCookieString!!
)
}
}
}
}

View file

@ -7,6 +7,17 @@ import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST
data class AuthPayload(
val authCookieString: String,
val authToken: String
)
data class SignInParams(
val token: String,
val provider: String, // APPLE or GOOGLE
val source: String = "ANDROID"
)
data class EmailAuthPayload(
val authCookieString: String?,
val authToken: String?,
@ -24,6 +35,12 @@ interface EmailLoginSubmit {
suspend fun submitEmailLogin(@Body credentials: EmailLoginCredentials): Response<EmailAuthPayload>
}
interface AuthProviderLoginSubmit {
@Headers("Content-Type: application/json")
@POST("/api/mobile-auth/sign-in")
suspend fun submitAuthProviderLogin(@Body params: SignInParams): Response<AuthPayload>
}
object RetrofitHelper {
fun getInstance(): Retrofit {
return Retrofit.Builder().baseUrl(Constants.demoProdURL)

View file

@ -12,6 +12,7 @@ GATEWAY_URL=http://localhost:4000/api
IMAGE_PROXY_URL=http://localhost:8080
IMAGE_PROXY_SECRET_KEY=some-secret
GAUTH_IOS_CLIENT_ID=
GAUTH_ANDROID_CLIENT_ID=
GAUTH_CLIENT_ID='notset'
GAUTH_SECRET='notset'
GCP_PROJECT_ID=omnivore-local

View file

@ -12,6 +12,7 @@ GATEWAY_URL=http://localhost:4000/api
IMAGE_PROXY_URL=http://localhost:8080
IMAGE_PROXY_SECRET_KEY=some-secret
GAUTH_IOS_CLIENT_ID=
GAUTH_ANDROID_CLIENT_ID=
GAUTH_CLIENT_ID='notset'
GAUTH_SECRET='notset'
GCP_PROJECT_ID=omnivore-local

View file

@ -57,16 +57,22 @@ export const validateGoogleUser = async (
}
}
const googleMobileClient = new OAuth2Client(env.google.auth.iosClientId)
const googleWebClient = new OAuth2Client(env.google.auth.clientId)
export async function decodeGoogleToken(
idToken: string
idToken: string,
isAndroid: boolean
): Promise<DecodeTokenResult> {
try {
const clientID = isAndroid
? env.google.auth.androidClientId
: env.google.auth.iosClientId
const googleMobileClient = new OAuth2Client(clientID)
const loginTicket = await googleMobileClient.verifyIdToken({
idToken,
audience: env.google.auth.iosClientId,
audience: clientID,
})
const email = loginTicket.getPayload()?.email

View file

@ -16,8 +16,9 @@ export function mobileAuthRouter() {
const router = express.Router()
router.post('/sign-in', async (req, res) => {
const { token, provider } = req.body
const payload = await createMobileSignInResponse(token, provider)
const { token, provider, source } = req.body
const isAndroid = source === 'ANDROID'
const payload = await createMobileSignInResponse(isAndroid, token, provider)
res.status(payload.statusCode).json(payload.json)
})

View file

@ -15,12 +15,13 @@ import { StatusType } from '../../../datalayer/user/model'
import { comparePassword } from '../../../utils/auth'
export async function createMobileSignInResponse(
isAndroid: boolean,
token?: string,
provider?: AuthProvider
): Promise<JsonResponsePayload> {
try {
if (token && provider === 'GOOGLE') {
const decodedTokenResult = await decodeGoogleToken(token)
const decodedTokenResult = await decodeGoogleToken(token, isAndroid)
return createAuthResponsePayload(provider, decodedTokenResult)
}

View file

@ -30,6 +30,7 @@ interface BackendEnv {
google: {
auth: {
iosClientId: string
androidClientId: string
clientId: string
secret: string
}
@ -122,6 +123,7 @@ const nullableEnvVars = [
'PREVIEW_GENERATION_SERVICE_URL',
'GCS_UPLOAD_SA_KEY_FILE_PATH',
'GAUTH_IOS_CLIENT_ID',
'GAUTH_ANDROID_CLIENT_ID',
'GAUTH_CLIENT_ID',
'GAUTH_SECRET',
'SEGMENT_WRITE_KEY',
@ -195,6 +197,7 @@ export function getEnv(): BackendEnv {
const google = {
auth: {
iosClientId: parse('GAUTH_IOS_CLIENT_ID'),
androidClientId: parse('GAUTH_ANDROID_CLIENT_ID'),
clientId: parse('GAUTH_CLIENT_ID'),
secret: parse('GAUTH_SECRET'),
},