redirect method
Redirects the response to the given path.
Determines whether the path is a full URL or a relative path. If the path is a relative path and
checkApiPath
is true, it prefixes the path with /api
. Performs the redirection with the specified
HTTP status code.
path
- The path or URL to redirect to.
status
- The HTTP status code to be used for the redirection. Default is 302 (moved temporarily).
checkApiPath
- A flag indicating whether to prefix the path with /api
if it's an API endpoint. Default is true.
Returns a Future<String> with a message indicating the redirection status.
Implementation
Future<String> redirect(
String path, {
int status = HttpStatus.movedTemporarily,
bool checkApiPath = true,
}) async {
if (isClosed) {
return '';
}
isClosed = true;
if (path.toLowerCase().startsWith("http") ||
path.toLowerCase().startsWith("https")) {
await response.redirect(
Uri.parse(path),
status: status,
);
return "Wait to redirect!?";
}
path = path.replaceAll('//', '/');
var uri = Uri.parse(path);
if (checkApiPath && isApiEndpoint) {
uri = uri.replace(pathSegments: ['api', ...uri.pathSegments]);
}
uri = uri.normalizePath();
await response.redirect(
uri,
status: status,
);
return "Wait to redirect!?";
}