The admin Auth Context
Several server-side APIs receive an admin object describing the current caller. The shape is the same everywhere — only the calling context differs.
Where you'll see it
- Security Rules — referenced as
admininside rule expressions (e.g.,"admin.uid == $userId") - Database Triggers — not directly received; triggers see the data change, not the caller
- Callable Functions — second argument:
function(data, admin, sessionId) - Connection Triggers — first argument:
function(admin, sessionId) - postWithBody handlers — exposed as
req.admin - Raw
posthandlers — returned bycheckAuth(req, res)
Object Shape
The admin object always contains request metadata. Identity fields (uid, username, token, claims) are present only when the caller has a valid session cookie.
| Property | When Authenticated | When Not Authenticated |
|---|---|---|
uid |
User's unique ID | undefined |
username |
User's username | undefined |
token |
Session token from cookie | undefined |
claims |
Custom claims object (e.g., { role: "admin" }) |
undefined |
urlParams |
Parsed query string parameters | Parsed query string parameters |
cookies |
Parsed cookies object | Parsed cookies object |
referer |
Referer header (or "") |
Referer header (or "") |
userAgent |
User-Agent header (or "") |
User-Agent header (or "") |
ip |
Client IP address | Client IP address |
url |
Request URL path | Request URL path |
Common Patterns
Reading admin in different contexts
// In a callable
addCallable("getProfile", (data, admin, sessionId) => {
if (!admin.uid) return { status: "Failed", message: "Login required" };
return get(["users", admin.uid]).data;
});
// In a connection trigger
addConnectionTrigger("open", (admin, sessionId) => {
console.log("Connected:", admin.uid || "anonymous", "from", admin.ip);
});
// In a postWithBody handler (req.admin)
nukebase.app.postWithBody("/api/me", (res, req) => {
if (!req.admin.uid) return res.send(JSON.stringify({ status: "Failed" }), "401 Unauthorized");
res.send(JSON.stringify({ uid: req.admin.uid, claims: req.admin.claims }));
});
// In a raw post handler (manual checkAuth)
nukebase.app.post("/api/me-raw", (res, req) => {
const admin = checkAuth(req, res);
res.end(JSON.stringify({ uid: admin.uid }));
});
// In a security rule (rules.js)
module.exports = {
"users": {
"$userId": {
"write": "admin.uid == $userId",
"private": { "read": "admin.uid == $userId" }
},
"adminPanel": {
"read": "admin.claims.role == 'admin'"
}
}
};
Anonymous callers still get an admin object. Identity fields will be undefined, but request metadata (ip, userAgent, cookies, etc.) is always populated. Always check admin.uid before assuming the caller is logged in.