refresh token api integreted and isLogin created in local storages

This commit is contained in:
mystery012728
2026-01-27 18:47:15 +05:30
parent f5782f6da1
commit 1cb344738e
13 changed files with 619 additions and 219 deletions

View File

@@ -41,12 +41,22 @@ class LocalDatabase {
/// LOGIN TABLE
await db.execute('''
CREATE TABLE login_state (
id INTEGER PRIMARY KEY,
is_login INTEGER NOT NULL
)
''');
CREATE TABLE login_state (
id INTEGER PRIMARY KEY,
is_logged_in INTEGER NOT NULL
)
''');
/// USER TOKENS TABLE
await db.execute('''
CREATE TABLE user_tokens (
id INTEGER PRIMARY KEY,
access_token TEXT NOT NULL,
refresh_token TEXT NOT NULL,
refresh_token_max_age INTEGER NOT NULL
)
''');
},
);
}
}
}

View File

@@ -91,36 +91,22 @@ class LocalPreference {
await updateOnboardingPage(0);
}
static Future<void> initLoginState() async {
final db = await LocalDatabase().database;
final result = await db.query('login_state');
if (result.isEmpty) {
await db.insert(
'login_state',
{
'id': 1,
'is_login': 0, // false by default
},
);
}
}
static Future<void> setIsLogin(bool value) async {
/// Set login state
static Future<void> setLogin(bool value) async {
final db = await LocalDatabase().database;
await db.insert(
'login_state',
{
'id': 1,
'is_login': value ? 1 : 0,
'is_logged_in': value ? 1 : 0,
},
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
static Future<bool> isLogin() async {
/// Get login state
static Future<bool> getLogin() async {
final db = await LocalDatabase().database;
final result = await db.query(
@@ -130,13 +116,72 @@ class LocalPreference {
);
if (result.isNotEmpty) {
return (result.first['is_login'] as int) == 1;
return result.first['is_logged_in'] == 1;
}
return false;
}
static Future<void> logout() async {
await setIsLogin(false);
/// Set user tokens
static Future<void> setTokens({
required String accessToken,
required String refreshToken,
required int refreshTokenMaxAge,
}) async {
final db = await LocalDatabase().database;
await db.insert(
'user_tokens',
{
'id': 1,
'access_token': accessToken,
'refresh_token': refreshToken,
'refresh_token_max_age': refreshTokenMaxAge,
},
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
}
/// Get access token
static Future<String?> getAccessToken() async {
final db = await LocalDatabase().database;
final result = await db.query(
'user_tokens',
where: 'id = ?',
whereArgs: [1],
);
if (result.isNotEmpty) {
return result.first['access_token'] as String?;
}
return null;
}
/// Get refresh token
static Future<String?> getRefreshToken() async {
final db = await LocalDatabase().database;
final result = await db.query(
'user_tokens',
where: 'id = ?',
whereArgs: [1],
);
if (result.isNotEmpty) {
return result.first['refresh_token'] as String?;
}
return null;
}
/// Clear tokens (for logout)
static Future<void> clearTokens() async {
final db = await LocalDatabase().database;
await db.delete(
'user_tokens',
where: 'id = ?',
whereArgs: [1],
);
}
}