Sign In with Device
Our Flutter SDK offers the easiest way to integrate Cotter's Sign In with Device. You can simply call a function and it does most of the heavy lifting and authentication for you.
Concepts: Learn about how Sign in with Device works.
There are major updates planned for this feature. Contact us in Slack so we can help you prepare for it.

What you're building

Sign In with Device using Cotter's Flutter SDK

Import Cotter as a dependency

Add Cotter to your pubspec.yaml , then run flutter pub get.
1
dependencies:
2
cotter:
Copied!
Check the latest releases in pub.dev. You may need to restart your flutter for it to run pod install (stop flutter run and re run it).
For Android: Update minSdkVersion to 18 following the installation instructions.

Step 1: Import and Initialize Cotter

Import Cotter in your lib/main.dart, then initialize it inside HomePageState.
1
import 'package:cotter/cotter.dart'; // Import Cotter
2
​
3
class HomePageState extends State {
4
...
5
​
6
// 1️⃣ Initialize Cotter
7
Cotter cotter = new Cotter(apiKeyID: API_KEY_ID); // πŸ‘ˆ Specify your API KEY ID here
8
​
9
// 2️⃣ TODO: Make Sign Up Function
10
// 3️⃣ TODO: Make Login Function
11
12
@override
13
Widget build(BuildContext context) { ... }
14
}
Copied!
You can create a free account at Cotter to get your API_KEY_ID.
Make sure you allow Trusted Devices Method in the dashboard.

Step 2: Make a Sign Up Function

To sign up, call cotter.signUpWithDevice and input the user's email. This will create a new user in Cotter and trust the current device to allow logins. This function returns the User object.
1
// 2️⃣ Make Sign Up Function
2
void signUp(BuildContext context) async {
3
try {
4
// πŸš€ One-line Sign Up
5
// Create a new user in Cotter and trust the current device
6
var user = await cotter.signUpWithDevice(identifier: inputController.text);
7
print(user);
8
} catch (e) {
9
print(e);
10
}
11
}
Copied!

(Optional) You can also verify the user's email or phone number at this step

To verify the user's email or phone number, follow this guide after you've finished this one.

Step 3: Make a Login Function

To sign in, call cotter.signInWithDevice . If the user is logging-in from a device that they trust, they'll automatically be approved. This function returns the Event object.
1
// 3️⃣ Make Login Function
2
void login(BuildContext context) async {
3
try {
4
// πŸš€ One-line Login
5
var event = await cotter.signInWithDevice(identifier: inputController.text, context: context);
6
print(event);
7
} catch (e) {
8
print(e);
9
}
10
}
Copied!

To get the Logged-in User Info and Access Token, follow this section:

Logging-in From a Trusted Device

If the user logged-in from a trusted device, their login request will automatically be approved.

Logging-in From a Non-Trusted Device

If the user logged-in from a different device that is not trusted, they will be presented with a prompt asking them to approve the login from their trusted phone inside your app. We'll cover this in the next guide.

Validating Cotter's Access Token

Checkout how to verify the OAuth Tokens from Cotter here:

πŸŽ‰ You're done!

Add Email/Phone Verification

You can also verify your user's email or phone number during the sign up process.

Getting the Logged-in User

Cotter's SDK automatically saves the logged-in user in your device's secure storage. Check out how to get the user information:

Getting OAuth Tokens

Cotter also automatically generates an access_token, id_token , and refresh_token that is securely stored in the device's secure storage. Check how to get these tokens:
Last modified 1yr ago