diff --git a/src/lib/ae_core/ae_core_functions.ts b/src/lib/ae_core/ae_core_functions.ts index 42b42252..07ea34a2 100644 --- a/src/lib/ae_core/ae_core_functions.ts +++ b/src/lib/ae_core/ae_core_functions.ts @@ -21,6 +21,8 @@ import { import { auth_ae_obj__username_password, auth_ae_obj__user_id_user_auth_key, + send_email_auth_ae_obj__user_id, + qry_ae_obj_li__user_email, // handle_load_ae_obj_id__user, // handle_load_ae_obj_li__user, // handle_create_ae_obj__user, @@ -442,6 +444,8 @@ let export_obj = { handle_update_ae_obj__person: handle_update_ae_obj__person, auth_ae_obj__username_password: auth_ae_obj__username_password, auth_ae_obj__user_id_user_auth_key: auth_ae_obj__user_id_user_auth_key, + send_email_auth_ae_obj__user_id: send_email_auth_ae_obj__user_id, + qry_ae_obj_li__user_email: qry_ae_obj_li__user_email, handle_update_ae_obj_id_crud: handle_update_ae_obj_id_crud, handle_download_export__obj_type: handle_download_export__obj_type, generate_qr_code: generate_qr_code diff --git a/src/lib/ae_core/core__user.ts b/src/lib/ae_core/core__user.ts index e58ef61f..f65567de 100644 --- a/src/lib/ae_core/core__user.ts +++ b/src/lib/ae_core/core__user.ts @@ -133,4 +133,149 @@ export async function auth_ae_obj__user_id_user_auth_key( console.log('ae_promises.auth__user_id_user_key:', ae_promises.auth__user_id_user_key); } return ae_promises.auth__user_id_user_key; -} \ No newline at end of file +} + + +// Send an email to the user with a new one time use authentication key. The new key must be generated and returned first. +// Updated 2025-04-08 +export async function send_email_auth_ae_obj__user_id( + { + api_cfg, + account_id, + user_id, + base_url, + key_param_name = 'user_key', // API defaults to 'auth_key' + params = {}, + // try_cache = true, + log_lvl = 1 + }: { + api_cfg: any, + account_id: string, + user_id: string, + base_url?: string, + key_param_name?: string, + params?: key_val, + // try_cache?: boolean, + log_lvl?: number + } + ) { + if (log_lvl) { + console.log(`*** send_email_auth_ae_obj__user_id() *** account_id=${account_id} user_id=${user_id}`); + } + if (log_lvl > 1) { + console.log(api_cfg); + } + + let email_auth_key_endpoint = `user/${user_id}/email_auth_key_url`; + params = { + 'root_url': base_url, + 'key_param_name': key_param_name + } + ae_promises.auth_key__send_email = await api.get_object({ + api_cfg: api_cfg, + endpoint: email_auth_key_endpoint, + params: params, + log_lvl: log_lvl + }); + + return ae_promises.auth_key__send_email; + + // let endpoint = `/user/${user_id}/new_auth_key`; + + // // params['user_id'] = user_id; // Required + // if (log_lvl > 1) { + // console.log(`send_email_auth_ae_obj__user_id() - params:`, params); + // } + + // ae_promises.auth_key__gen_auth_key = await api.get_object({ + // api_cfg: api_cfg, + // endpoint: endpoint, + // params: params, + // log_lvl: log_lvl + // }) + // .then(async function (email_send_result) { + // if (email_send_result) { + // let email_auth_key_endpoint = `user/${user_id}/email_auth_key_url`; + // params = { + // 'root_url': 'https://test.oneskyit.com' + // } + // ae_promises.auth_key__send_email = await api.get_object({ + // api_cfg: api_cfg, + // endpoint: email_auth_key_endpoint, + // params: params, + // log_lvl: log_lvl + // }) + + // return email_send_result; + // } else { + // console.log('No results returned.'); + // return null; + // } + // }) + // .catch(function (error) { + // console.log('No results returned or failed.', error); + // }); + + // if (log_lvl) { + // console.log('ae_promises.send_email_auth__user_id:', ae_promises.send_email_auth__user_id); + // } + // return ae_promises.send_email_auth__user_id; +} + +// Look up user based on email address provided +// Updated 2025-04-08 +export async function qry_ae_obj_li__user_email( + { + api_cfg, + account_id, + null_account_id = false, + email, + params = {}, + try_cache = true, + log_lvl = 1 + }: { + api_cfg: any, + account_id: string, + null_account_id?: boolean, + email: string, + params?: key_val, + try_cache?: boolean, + log_lvl?: number + } + ) { + if (log_lvl) { + console.log(`*** qry_ae_obj_li__user_email() *** account_id=${account_id} email=${email}`); + } + + // /user/lookup_email + let endpoint = '/user/lookup_email'; + + params['email'] = email; // Required + params['null_account_id'] = null_account_id || false; + if (log_lvl > 1) { + console.log(`qry_ae_obj_li__user_email() - params:`, params); + } + + ae_promises.qry__user_email = await api.get_object({ + api_cfg: api_cfg, + endpoint: endpoint, + params: params, + log_lvl: log_lvl + }) + .then(async function (user_obj_get_result) { + if (user_obj_get_result) { + return user_obj_get_result; + } else { + console.log('No results returned.'); + return null; + } + }) + .catch(function (error) { + console.log('No results returned or failed.', error); + }); + + if (log_lvl) { + console.log('ae_promises.qry__user_email:', ae_promises.qry__user_email); + } + return ae_promises.qry__user_email; +} diff --git a/src/lib/element_access_type.svelte b/src/lib/element_access_type.svelte index 1eb75e03..9e43c0e3 100644 --- a/src/lib/element_access_type.svelte +++ b/src/lib/element_access_type.svelte @@ -1,11 +1,15 @@ diff --git a/src/lib/element_sign_in_out.svelte b/src/lib/element_sign_in_out.svelte index 58588763..aaf352b4 100644 --- a/src/lib/element_sign_in_out.svelte +++ b/src/lib/element_sign_in_out.svelte @@ -5,8 +5,10 @@ import { browser } from '$app/environment'; // *** Import other supporting libraries import { + CircleX, LogIn, LogOut, LockKeyhole, - User + Mail, MailCheck, + User, UserCheck } from '@lucide/svelte'; // *** Import Aether specific variables and functions @@ -31,6 +33,8 @@ let { let url_user_id = data.url.searchParams.get('user_id'); let url_user_key = data.url.searchParams.get('user_key'); // Reminder that "key" is the site's auth key. +let url_user_username = data.url.searchParams.get('username'); +let url_user_email = data.url.searchParams.get('user_email'); let ae_promises: key_val = {}; @@ -118,6 +122,62 @@ if (browser) { // Pre-fill the auth__entered_key if passed in via the URL. $ae_sess.auth__entered_user_key = url_user_key; } + if (url_user_username) { + // Pre-fill the auth__entered_username if passed in via the URL. + $ae_sess.auth__entered_username = url_user_username; + } + if (url_user_email) { + // Pre-fill the auth__entered_email if passed in via the URL. + $ae_sess.auth__entered_email = url_user_email; + } +} + + + +// Use core_func.send_email_auth_ae_obj__user_id +function handle_send_auth_email({user_id}: {user_id: string}) { + console.log('handle_send_auth_email()'); + + console.log($ae_loc.base_url); // URL origin + console.log($ae_loc.hostname); // URL hostname + + // This function creates a new auth_key and then sends an email to the user with the new auth key. + ae_promises.send_email_auth_ae_obj__user_id = core_func.send_email_auth_ae_obj__user_id({ + api_cfg: $ae_api, + account_id: $slct.account_id, + user_id: user_id, + base_url: $ae_loc.base_url, + log_lvl: 2 + }); + +} + +// Use core_func.qry_ae_obj_li__user_email +function handle_lookup_user_email({email}: {email: string}) { + console.log('handle_lookup_user_email()'); + + // If a user is returned then send the new auth key email + ae_promises.load__user_obj_li = core_func.qry_ae_obj_li__user_email({ + api_cfg: $ae_api, + account_id: $slct.account_id, + null_account_id: false, + email: email, + log_lvl: 1 + }).then((user_response) => { + if (user_response?.user_id_random) { + console.log(`User found for email:`, user_response); + handle_send_auth_email({ + user_id: user_response.user_id_random + }); + } else if (user_response.length > 0) { + console.log(`Multiple users found for email:`, user_response); + handle_send_auth_email({ + user_id: user_response[0].user_id_random + }); + } else { + alert('No user found with that email address.'); + } + }); } @@ -138,7 +198,7 @@ if (browser) { > + + + +
{ e.preventDefault(); @@ -418,11 +527,12 @@ if (browser) {
diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts index a7f8e1e5..02761b52 100644 --- a/src/routes/+layout.ts +++ b/src/routes/+layout.ts @@ -125,11 +125,14 @@ export async function load({ fetch, params, parent, route, url }) { // params, r submenu: {}, }; + // let parent_data = await parent(); + // console.log(`root +layout.ts parent_data:`, parent_data); + // First do a site_domain look up to check if it is valid and get the account_id. // ae_loc.url_host = data.url.host; // Use this to look up? sub.example.com:123 // ae_loc.fqdn = url.host; // Use this to look up? sub.example.com:123 - // ae_loc.url_hostname = data.url.hostname; // sub.example.com - // ae_loc.url_origin = data.url.origin; // Use this to look up? https://sub.example.com:123 + // ae_loc.url_hostname = parent_data.url.hostname; // sub.example.com + // ae_loc.url_origin = parent_data.url.origin; // Use this to look up? https://sub.example.com:123 // ae_loc.site_domain = data.url.origin; // console.log(`ae_loc = `, ae_loc); @@ -200,6 +203,9 @@ export async function load({ fetch, params, parent, route, url }) { // params, r ae_loc_init['site_access_key'] = json_data.access_key; // This is the general site access key ae_loc_init['site_domain_access_key'] = json_data.site_domain_access_key; // This is specific to a (sub)domain. + ae_loc_init['base_url'] = url.origin; + ae_loc_init['hostname'] = url.hostname + if (!ae_loc_init['site_access_key'] && !ae_loc_init['site_domain_access_key']) { ae_loc_init['key_checked'] = true; // Se to true to allow access without a key. ae_loc_init['allow_access'] = true; // No access key is required here.