A Neovim plugin enabling you to solve LeetCode problems.
| 1 | local utils = require("leetcode.api.utils") |
| 2 | local config = require("leetcode.config") |
| 3 | local log = require("leetcode.logger") |
| 4 | local queries = require("leetcode.api.queries") |
| 5 | local urls = require("leetcode.api.urls") |
| 6 | |
| 7 | ---@class lc.AuthApi |
| 8 | local Auth = {} |
| 9 | |
| 10 | ---@return lc.UserStatus, lc.err |
| 11 | |
| 12 | local query = queries.auth |
| 13 | |
| 14 | if cb then |
| 15 | utils.query(query, {}, { |
| 16 | callback = function(res, err) |
| 17 | cb(Auth.handle(res, err)) |
| 18 | end, |
| 19 | endpoint = urls.auth, |
| 20 | }) |
| 21 | else |
| 22 | return Auth.handle(utils.query(query, {}, { |
| 23 | endpoint = urls.auth, |
| 24 | })) |
| 25 | end |
| 26 | end |
| 27 | |
| 28 | ---@private |
| 29 | ---@return lc.UserStatus, lc.err |
| 30 | |
| 31 | if err then |
| 32 | return res, err |
| 33 | end |
| 34 | |
| 35 | local auth = res.data.userStatus |
| 36 | err = {} |
| 37 | |
| 38 | if (not config.is_cn and auth.id == vim.NIL) or (config.is_cn and auth.slug == vim.NIL) then |
| 39 | err.msg = "Cookie expired?" |
| 40 | elseif not auth.is_signed_in then |
| 41 | err.msg = "Sign-in failed" |
| 42 | elseif not auth.is_verified then |
| 43 | err.msg = "Please verify your email address in order to use your account" |
| 44 | end |
| 45 | |
| 46 | if err.msg then |
| 47 | require("leetcode.command").delete_cookie() |
| 48 | return nil, err |
| 49 | end |
| 50 | |
| 51 | config.auth = log.debug(auth) ---@diagnostic disable-line |
| 52 | return auth |
| 53 | end |
| 54 | |
| 55 | return Auth |