Skip to content

Commit

Permalink
refactor: Move social logins API
Browse files Browse the repository at this point in the history
  • Loading branch information
cogk committed Oct 17, 2024
1 parent a90f2a8 commit 38f7854
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
2 changes: 1 addition & 1 deletion frontend/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async function submit(e) {
}
const authProviders = createResource({
url: "hrms.www.hrms.oauth_providers",
url: "hrms.api.oauth.oauth_providers",
auto: true,
});
</script>
40 changes: 40 additions & 0 deletions hrms/api/oauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import frappe


@frappe.whitelist(allow_guest=True)
def oauth_providers():
from frappe.utils.html_utils import get_icon_html
from frappe.utils.oauth import get_oauth2_authorize_url, get_oauth_keys
from frappe.utils.password import get_decrypted_password

out = []
providers = frappe.get_all(
"Social Login Key",
filters={"enable_social_login": 1},
fields=["name", "client_id", "base_url", "provider_name", "icon"],
order_by="name",
)

for provider in providers:
client_secret = get_decrypted_password("Social Login Key", provider.name, "client_secret")
if not client_secret:
continue

icon = None
if provider.icon:
if provider.provider_name == "Custom":
icon = get_icon_html(provider.icon, small=True)
else:
icon = f"<img src='{provider.icon}' alt='{provider.provider_name}'>"

if provider.client_id and provider.base_url and get_oauth_keys(provider.name):
out.append(
{
"name": provider.name,
"provider_name": provider.provider_name,
"auth_url": get_oauth2_authorize_url(provider.name, "/hrms"),
"icon": icon,
}
)

return out
44 changes: 4 additions & 40 deletions hrms/www/hrms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,8 @@ def get_context_for_dev():

def get_boot():
return frappe._dict(
{"site_name": frappe.local.site, "push_relay_server_url": frappe.conf.get("push_relay_server_url") or ""}
{
"site_name": frappe.local.site,
"push_relay_server_url": frappe.conf.get("push_relay_server_url") or "",
}
)


@frappe.whitelist(allow_guest=True)
def oauth_providers():
from frappe.utils.html_utils import get_icon_html
from frappe.utils.oauth import get_oauth2_authorize_url, get_oauth_keys
from frappe.utils.password import get_decrypted_password

out = []
providers = frappe.get_all(
"Social Login Key",
filters={"enable_social_login": 1},
fields=["name", "client_id", "base_url", "provider_name", "icon"],
order_by="name",
)

for provider in providers:
client_secret = get_decrypted_password("Social Login Key", provider.name, "client_secret")
if not client_secret:
continue

icon = None
if provider.icon:
if provider.provider_name == "Custom":
icon = get_icon_html(provider.icon, small=True)
else:
icon = f"<img src='{provider.icon}' alt='{provider.provider_name}'>"

if provider.client_id and provider.base_url and get_oauth_keys(provider.name):
out.append(
{
"name": provider.name,
"provider_name": provider.provider_name,
"auth_url": get_oauth2_authorize_url(provider.name, "/hrms"),
"icon": icon,
}
)

return out

0 comments on commit 38f7854

Please sign in to comment.