Skip to content

Add type checking, improve errors, better method naming, etc... #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions AuthAPI.lua

This file was deleted.

79 changes: 79 additions & 0 deletions src/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--[[
Authors:
deprecatedbrain (co_existance) (12/11/2021), -- Creating the original file
LucasMZ (12/12/2021), -- Type annotation & checking, error improvements...
]]

local HttpService = game:GetService("HttpService")

local AuthAPI = {}

local AUTHENTICATION_URL = "https://www.authenticatorapi.com/Validate.aspx?Pin=%s&SecretCode=%s"
local QRCODE_URL = "https://www.authenticatorapi.com/pair.aspx?AppName=%s&AppInfo=%s&SecretCode=%s"

function AuthAPI.CheckPinAsync(
code: string | number,
secret: string
): boolean

assert(
tonumber(code) ~= nil,
"Code must be number"
)

assert(
typeof(secret) == 'string',
"Secret must be string"
)

local requestUrl = string.format(
AUTHENTICATION_URL,
code, secret
)

local wasRequestSuccessful, result = pcall(
HttpService.GetAsync, HttpService,
requestUrl
)

if wasRequestSuccessful then
if result == "True" then
return true
elseif result == "False" then
return false
else
error("Invalid result returned\n Result: ".. result)
end
else
error("Remote request failed\n Error: ".. result)
end
end

function AuthAPI.GetQRCodeURL(
appName: string,
appInfo: string,
secret: string
): string

assert(
typeof(appName) == 'string',
"AppName must be string"
)

assert(
typeof(appInfo) == 'string',
"AppInfo must be string"
)

assert(
typeof(secret) == 'string',
"Secret must be string"
)

return string.format(
QRCODE_URL,
appName, appInfo, secret
)
end

return AuthAPI