|
| 1 | +### Get Current User |
| 2 | + |
| 3 | +This call fetches the current logged in user and should be used after a user has been successfully signed in. |
| 4 | +If the user is signed in, it will return the current userId and username. |
| 5 | +Note: An empty string will be assigned to userId and/or username, if the values are not present in the accessToken. |
| 6 | + |
| 7 | +<BlockSwitcher> |
| 8 | + |
| 9 | +<Block name="Java"> |
| 10 | + |
| 11 | +```java |
| 12 | + try { |
| 13 | + Amplify.Auth.getCurrentUser( |
| 14 | + result -> Log.i("AuthQuickstart", "Current user details are:" + result.toString(), |
| 15 | + error -> Log.e("AuthQuickstart", "getCurrentUser failed with an exception: " + error) |
| 16 | + ); |
| 17 | + } catch (Exception error) { |
| 18 | + Log.e("AuthQuickstart", "unexpected error: " + error); |
| 19 | + } |
| 20 | +``` |
| 21 | +</Block> |
| 22 | + |
| 23 | +<Block name="Kotlin - Callbacks"> |
| 24 | + |
| 25 | +```kotlin |
| 26 | +Amplify.Auth.getCurrentUser({ |
| 27 | + Log.i("AuthQuickStart", "Current user details are: $it")},{ |
| 28 | + Log.e("AuthQuickStart", "getCurrentUser failed with an exception: $it") |
| 29 | +}) |
| 30 | +``` |
| 31 | +</Block> |
| 32 | + |
| 33 | +<Block name="Kotlin - Coroutines"> |
| 34 | + |
| 35 | +```kotlin |
| 36 | +try { |
| 37 | + val result = Amplify.Auth.getCurrentUser() |
| 38 | + Log.i("AuthQuickstart", "Current user details are: $result") |
| 39 | +} catch (error: Exception) { |
| 40 | + Log.e("AuthQuickstart", "getCurrentUser failed with an exception: $error") |
| 41 | +} |
| 42 | +``` |
| 43 | +</Block> |
| 44 | + |
| 45 | +<Block name="RxJava"> |
| 46 | + |
| 47 | +```java |
| 48 | + RxAmplify.Auth.getCurrentUser().subscribe( |
| 49 | + result -> Log.i("AuthQuickStart getCurrentUser: " + result.toString()), |
| 50 | + error -> Log.e("AuthQuickStart", error.toString()) |
| 51 | + ); |
| 52 | +``` |
| 53 | +</Block> |
| 54 | + |
| 55 | +</BlockSwitcher> |
0 commit comments