implement auth remove action

This commit is contained in:
yggverse 2024-11-20 19:02:12 +02:00
parent a01b3283d4
commit 62c21523df
2 changed files with 62 additions and 40 deletions

View File

@ -82,27 +82,43 @@ impl Gemini {
widget.on_apply({ widget.on_apply({
let widget = widget.clone(); let widget = widget.clone();
move |response| { move |response| {
// Get record ID depending of user selection // Get option match user choice
let profile_identity_gemini_id = match response { let option = match response {
Value::PROFILE_IDENTITY_GEMINI_ID(value) => value, Value::PROFILE_IDENTITY_GEMINI_ID(value) => Some(value),
Value::USE_GUEST_SESSION => todo!(), Value::USE_GUEST_SESSION => None,
Value::GENERATE_NEW_AUTH => profile Value::GENERATE_NEW_AUTH => Some(
.identity profile
.gemini .identity
.create(None, widget.form.name.value().as_deref()) .gemini
.unwrap(), // @TODO .create(None, widget.form.name.value().as_deref())
.unwrap(), // @TODO handle result,
),
}; };
// Activate identity for given `auth_uri` // Apply
match profile match option {
.identity // Activate identity for `auth_uri`
.gemini Some(profile_identity_gemini_id) => {
.auth profile
.activate(profile_identity_gemini_id, auth_url.as_str()) .identity
{ .gemini
Ok(_) => action.reload().activate(), .auth
Err(reason) => todo!("{:?}", reason), .add(profile_identity_gemini_id, auth_url.as_str())
.unwrap();
}
// Remove all identity auths for `auth_uri`
None => {
profile
.identity
.gemini
.auth
.remove(auth_url.as_str())
.unwrap();
}
} }
// Update page
action.reload().activate();
} }
}); });

View File

@ -41,38 +41,44 @@ impl Auth {
/// * deactivate active auth by remove previous records from `Self` database /// * deactivate active auth by remove previous records from `Self` database
/// * reindex `Self` memory index on success /// * reindex `Self` memory index on success
/// * return last insert `profile_identity_gemini_auth_id` on success /// * return last insert `profile_identity_gemini_auth_id` on success
pub fn activate(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> { pub fn add(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
// Get all records match request // Cleanup records match `url` (unauthorize)
self.remove(url)?;
// Create new record (auth)
let profile_identity_gemini_auth_id =
match self.database.add(profile_identity_gemini_id, url) {
Ok(id) => id,
Err(reason) => {
return Err(Error::DatabaseRecordCreate(
profile_identity_gemini_id,
url.to_string(),
reason,
))
}
};
// Reindex
self.index()?;
// Done
Ok(profile_identity_gemini_auth_id)
}
/// Remove all records match request (unauthorize)
pub fn remove(&self, url: &str) -> Result<(), Error> {
match self.database.records(Some(url)) { match self.database.records(Some(url)) {
Ok(records) => { Ok(records) => {
// Cleanup records match `url` (unauth)
for record in records { for record in records {
if let Err(reason) = self.database.delete(record.id) { if let Err(reason) = self.database.delete(record.id) {
return Err(Error::DatabaseRecordDelete(record.id, reason)); return Err(Error::DatabaseRecordDelete(record.id, reason));
} }
} }
// Create new record (auth)
let profile_identity_gemini_auth_id =
match self.database.add(profile_identity_gemini_id, url) {
Ok(id) => id,
Err(reason) => {
return Err(Error::DatabaseRecordCreate(
profile_identity_gemini_id,
url.to_string(),
reason,
))
}
};
// Reindex
self.index()?;
// Done
Ok(profile_identity_gemini_auth_id)
} }
Err(reason) => return Err(Error::DatabaseRecordsRead(url.to_string(), reason)), Err(reason) => return Err(Error::DatabaseRecordsRead(url.to_string(), reason)),
} }
self.index()?;
Ok(())
} }
/// Create new `Memory` index from `Database` for `Self` /// Create new `Memory` index from `Database` for `Self`